【问题标题】:How to join tables on column name in sequelize?如何在 sequelize 中加入列名上的表?
【发布时间】:2017-02-03 19:45:34
【问题描述】:

是否有任何可能的方法来重现此查询

select table2.node_id as id, table2.client_id as clientId, table1.collection_id as collectionId from table1 inner join table2 on table2.primary_collection_id = table1.collection_id  

在sequelize orm中?

我定义了hasOnebelongsTo 关系并尝试这样做:

return Models.FileModel.findAll({
    include: [
        { model: Models.NodeModel, required: true}
    ]
});

但我的inner join 条件不正确: inner join table2 on table2.node_id = table1.collection_d

需要将table2.node_id替换为table2.primary_collection_id

【问题讨论】:

  • 请包含您设置表之间关系的代码。

标签: mysql node.js sequelize.js


【解决方案1】:

嗯,有时在阅读文档时需要更加专心。 我在找targetKey

所以我定义了关联

FileModel.belongsTo(NodeModel, {foreignKey: 'collection_id', targetKey: 'primary_collection_id'});

它对我有用

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 1970-01-01
    • 2017-07-02
    • 2021-08-29
    • 1970-01-01
    • 2021-03-24
    • 2022-12-11
    • 2020-12-24
    • 1970-01-01
    相关资源
    最近更新 更多