如何避免对同时包含外键和包含对象的 JS 关联进行 Sequelize [英] How to Avoid Sequelize JS Associations Including both the Foreign Key and the Included Object

查看:38
本文介绍了如何避免对同时包含外键和包含对象的 JS 关联进行 Sequelize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有以下模型结构:

表单输入:

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

我在阅读了 SequelizeJS 文档后对其进行了建模,并得出以下结论:

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"
    }
}

推荐答案

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

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

这篇关于如何避免对同时包含外键和包含对象的 JS 关联进行 Sequelize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆