在非主键上按顺序加入 [英] Sequelize Join on Non Primary Key
问题描述
我需要关联两个表. TableA有一个TableB.我可以在TableA模型中做到这一点:
I have two tables I need to associate. TableA has One TableB. I'm able to do this in the TableA Model:
TableA.hasOne( models.TableB, { as: 'TableB', foreignKey: 'someID' } );
看看SQL,这会尝试联接TableA.ID和TableB.someID.我真正想要的是加入TableA.someNonPrimaryKey和TableB.someID.
Looking at the SQL, this tries to join TableA.ID and TableB.someID. What I actually want, is to join TableA.someNonPrimaryKey and TableB.someID.
我如何告诉sequelize加入someNonPrimaryKey?
How can I tell sequelize to join with someNonPrimaryKey?
推荐答案
目前暂时无法进行续集,但是您可以尝试这种方式.
This is not possible in sequelize at the moment, however you can try this way.
HasOne与BelongsTo之间的区别是,HasOne在目标模型中插入关联密钥,而BelongsTo在源模型中插入关联密钥. 关联而不是hasOne等于BelongsTo
Difference between HasOne and BelongsTo, HasOne inserts the association key in target model whereas BelongsTo inserts the association key in the source model. association rather hasOne equals BelongsTo
TableB.belongsTo(models.TableA, { as: 'TableA', foreignKey: 'someID', targetKey: 'notTableAID' } );
这篇关于在非主键上按顺序加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!