如何在sequelize中连接列名列表?

时间:2017-02-03 11:26:09

标签: mysql node.js sequelize.js

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

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

1 个答案:

答案 0 :(得分:0)

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

所以我定义了关联

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

它对我有用