如何避免Sequelize JS关联包括外键和包含的对象

时间:2017-03-01 23:16:59

标签: javascript node.js sequelize.js

如何避免同时显示sequelize创建的foreignKey和通过include包含的热切对象?

我有以下模型结构:

FormEntry:

  • 所有者:用户
  • createdBy:用户
  • modifiedBy:用户
  • formEntryData:[FormEntryData]

我通过阅读SequelizeJS文档后对其进行了建模,并提出了以下建议:

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
});

然后我需要创建关联以将模型绑定在一起,经过大量的反复试验后,我想出了以下内容:

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'});

然后,我可以通过执行以下操作来查询数据:

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

不幸的是,我的结果似乎有点重复,因为它似乎包括外键和急切获取的对象。

{
    "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 个答案:

答案 0 :(得分:0)

您需要从查询中排除指定的字段

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'] }
})