【问题标题】:How to Avoid Sequelize JS Associations Including both the Foreign Key and the Included Object如何避免包含外键和包含对象的 Sequelize JS 关联
【发布时间】:2017-07-21 12:03:44
【问题描述】:

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

我有以下模型结构:

表单条目:

  • 所有者:用户
  • 创建者:用户
  • 修改者:用户
  • 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"
    }
}

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 2018-07-13
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2020-03-29
      相关资源
      最近更新 更多