How to Avoid Sequelize JS Associations Including both the Foreign Key and the Included Object

跟風遠走 提交于 2021-02-08 07:38:35

问题


How can I avoid showing both the foreignKey that sequelize creates and the eagerly fetched object through includes?

I have the following model structure:

FormEntry:

  • owner: User
  • createdBy: User
  • modifiedBy: User
  • formEntryData: [FormEntryData]

I modeled it after reading through SequelizeJS docs and came up with the following:

const User = sequelize.define('user', {
    id: {
        type: Sequelize.BIGINT(20),
        field: 'user_id',
        primaryKey: true
    },
    emailAddress: {
        type: Sequelize.STRING(256),
        field: 'email_address'
    }
}, {
    tableName: 'users',
    timestamps: false
});

const FormEntryData = sequelize.define('formEntryData', {
    id: {
        type: Sequelize.BIGINT(20),
        field: 'id',
        primaryKey: true
    },
    entryId: {
        type: Sequelize.BIGINT(20),
        field: 'entry_id'
    },
    ...
}, {
    tableName: 'formEntryData',
    timestamps: false
});

const FormEntry = sequelize.define('formEntry', {
    id: {
        type: Sequelize.BIGINT(20),
        field: 'entry_id',
        primaryKey: true
    },
    ...
}, {
    tableName: 'formEntries',
    timestamps: false
});

I then need to create the associations to tie the models together and after a lot of trial and error I came up with the following:

FormEntry.hasMany(FormEntryData, {foreignKey: 'entry_id', as: 'FormEntryData'});
FormEntry.belongsTo(User, {foreignKey: 'created_by', as: 'CreatedBy'});
FormEntry.belongsTo(User, {foreignKey: 'modified_by', as: 'ModifiedBy'});
FormEntry.belongsTo(User, {foreignKey: 'owner', as: 'Owner'});

I then was able to query the data by doing the following:

FormEntry.findByPrimary(1472280, {
    include: [
        {
            model: FormEntryData,
            as: "FormEntryData"
        },
        {
            model: User,
            as: "CreatedBy"
        },
        {
            model: User,
            as: "Owner"
        },
        {
            model: User,
            as: "ModifiedBy"
        }
    ]
})

Unfortunately, my results seem kind of repetitive as it seems to be including both the foreign key and the object that is eagerly fetched.

{
    "id": 1472280,
    ...
    "created_by": 26508,  <-- repetitive (don't want this)
    "modified_by": 26508, <-- repetitive (don't want this)
    "owner": null,        <-- repetitive (don't want this)
    "FormEntryData": [
        {
            "id": 27164476,
            "entryId": 1472280, <-- repetitive (but I want this one)
            ...
            "entry_id": 1472280 <-- repetitive (don't want this)
        },
        ...
    ],
    "CreatedBy": {        <-- repetitive (but I want this one)
        "id": 26508,
        "emailAddress": "swaraj.kler@greywallsoftware.com"
    },
    "Owner": null,        <-- repetitive (but I want this one)
    "ModifiedBy": {       <-- repetitive (but I want this one)
        "id": 26508,
        "emailAddress": "swaraj.kler@greywallsoftware.com"
    }
}

回答1:


You need to exclude specified fields from the query

FormEntry.findByPrimary(1472280, {
    include: [
        {
            model: FormEntryData,
            as: "FormEntryData",
            attributes: { exclude: ['entry_id'] }
        },
        {
            model: User,
            as: "CreatedBy"
        },
        {
            model: User,
            as: "Owner"
        },
        {
            model: User,
            as: "ModifiedBy"
        }
    ],
    attributes: { exclude: ['owner', 'created_by', 'modified_by'] }
})


来源:https://stackoverflow.com/questions/42543964/how-to-avoid-sequelize-js-associations-including-both-the-foreign-key-and-the-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!