Sequelize Join on Non Primary Key

一个人想着一个人 提交于 2019-12-05 20:00:04

问题


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

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.

How can I tell sequelize to join with someNonPrimaryKey?


回答1:


This is not possible in sequelize at the moment, however you can try this way.

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


来源:https://stackoverflow.com/questions/28974021/sequelize-join-on-non-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!