【问题标题】:Can't exclude association's fields from select statement in sequelize无法从 sequelize 中的 select 语句中排除关联的字段
【发布时间】:2020-06-16 06:56:39
【问题描述】:

我有以下代码(简化):

var group = sequelize.define("group", {
    id: {type: DataTypes.INTEGER, autoIncrement: false, primaryKey: true},
    name: type: DataTypes.STRING,
    parentId: DataTypes.INTEGER
}, { classMethods: {
        associate: function (models) {
            group.belongsToMany(models.item, { as:'items', foreignKey: 'group_id', through: models.group_item_tie });
        }}
});

var group_item_tie = sequelize.define("group_item_tie", {}, {freezeTableName: true});

var item = sequelize.define("item", {
    spn: { type: DataTypes.INTEGER, autoIncrement: false, primaryKey: true },
}, { classMethods: {
        associate: function (models) {
            item.belongsToMany(models.group, { foreignKey: 'spn', through: models.group_item_tie });
        }}
});

当我尝试返回一些有关系的记录时,可以这样说:

dbcontext.group.findAll({
    where: { id: 6 },
    include: [{
                model: dbcontext.item,
                as: 'items',
                attributes: ['spn']
            }]
    })

我还从平局表group_item_tie 中获得结果字段:

[{
    "id": 6,
    "name": "abc",
    "parentId": 5,
    "createdAt": "2015-05-06T15:54:58.000Z",
    "updatedAt": "2015-05-06T15:54:58.000Z",
    "items": [
        {   "spn": 1,
            "group_item_tie": {
                "createdAt": "2015-05-06 15:54:58.000 +00:00",
                "updatedAt": "2015-05-06 15:54:58.000 +00:00",
                "group_id": 6,
                "spn": 1
            }
        },
        {   "spn": 2,
            "group_item_tie": {
                "createdAt": "2015-05-06 15:54:58.000 +00:00",
                "updatedAt": "2015-05-06 15:54:58.000 +00:00",
                "group_id": 6,
                "spn": 2
            }
        },

我在生成的 sql 查询中看到它。如何从选择语句中排除那些?我尝试了其他一些方法,但没有成功。

我希望有一些更干净的东西然后只是做:

delete item.group_item_tie;

【问题讨论】:

    标签: javascript node.js sqlite sequelize.js


    【解决方案1】:

    我会回答自己,因为它可能对将来的某人有用。所以根据#3664#2974#2975 的答案如下(感谢mickhansen):

    include: [{
      model: dbcontext.item,
      as: 'items',
      attributes: ['spn'],
      through: {
        attributes: []
      }        
    }]
    

    很快就会记录下来。

    【讨论】:

    • 我这辈子都无法让它工作......我们确定这能工作吗?
    • 为我工作@MirroredFate,如果你还有它,把你的代码贴在 pastebin 上,让我们看看。
    • @GustavoMeira 这里已经有问题了:github.com/sequelize/sequelize/issues/5590
    • 我只是在模型级别做了空属性,似乎在投影中排除了它
    • 它似乎仍然将它们包含在 select 语句中,只是在最终输出中删除了它们......
    【解决方案2】:

    我意识到这个帖子有点过时了,但由于它在 Google 搜索结果中的位置很高,而且我自己也很难找到答案,所以我想我会在这里添加它。

    如果您使用 Model.getAssociatedModel()Model.$get()(用于 sequelize-typescript),则列出的当前答案不适用于此用例。为了隐藏模型关联,您需要添加joinTableAttributes: []

    例子:

    Model.getAssociatedModel({
      joinTableAttributes: []
    })
    

    例子:

    Model.$get('property', <any>{
      joinTableAttributes: []
    });
    

    在本文发布时,joinTableAttributes 不包含在 sequelize-typescript 类型中,因此 &lt;any&gt;

    【讨论】:

      【解决方案3】:
      through: {
      attributes: []
      }   
      

      为我工作

      【讨论】:

      • 请详细说明您的答案以及它如何应用于原始问题。答案应包含足够的信息以提供完整的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      相关资源
      最近更新 更多