Sequelize Join Master Key

I have two tables that I need to relate. Table A has one tab. I can do this in the TableA model:

TableA.hasOne( models.TableB, { as: 'TableB', foreignKey: 'someID' } ); 

Looking at SQL, he is trying to connect to TableA.ID and TableB.someID. Actually I want to join TableA.someNonPrimaryKey and TableB.someID.

How can I say selizeize is joining someNonPrimaryKey?

+7
source share
1 answer

This is currently not possible in the future, but you can try this way.

The difference between HasOne and BelongsTo, HasOne inserts an association key into the target model, while BelongsTo inserts an association key into the original model. the association rather hasOne equals BelongsTo

 TableB.belongsTo(models.TableA, { as: 'TableA', foreignKey: 'someID', targetKey: 'notTableAID' } ); 
+2
source share

All Articles