问题
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