Repository navigation
Description
Please forgive me if I sound ignorant or something, it's been one year since I last used NodeJS and this awesome library.
What are you doing?
Users.hasOne(models.Bank, {
foreignKey: 'id',
targetKey: 'bank_id'
});This didn't work, but when I read the docu about hasOne, it suggested using sourceKey instead of the targetKey, so I changed the code to the following
Users.hasOne(models.Bank, {
foreignKey: 'id',
sourceKey: 'bank_id'
});And then it worked, but then I wondered, what's the difference?
From the docs:
The source key is the attribute on the source model that the foreign key attribute on the target model points to. By default the source key for a hasOne relation will be the source model's primary attribute. To use a custom attribute, use the sourceKey option.
The target key is the column on the target model that the foreign key column on the source model points to. By default the target key for a belongsTo relation will be the target model's primary key. To define a custom column, use the targetKey option.
But the way I see it is that these two are the same thing...
SourceModel.hasOne(TargetModel, {
foreignKey: 'id',
sourceKey: 'bank_id'
});
SourceModel.hasMany(TargetModel, {
foreignKey: 'id',
targetKey: 'bank_id'
});So basically both targetKey and sourceKey refer to the attribute to the SourceModel that could be used to identify the row we want to get.
What do you expect to happen?
targetKey should work for hasOne.
What is actually happening?
sourceKey was used instead but seems the same with targetKey.
Environment
Dialect:
- mysql
- postgres
- sqlite
- mssql
- any
Dialect library version: [email protected]
Database version: mysql Ver 15.1 Distrib 10.3.14-MariaDB
Sequelize version: 5.8.6
Node Version: v12.1.0
OS: MacOS Mojave
If TypeScript related: TypeScript version: XXX
Tested with latest release: - No
- Yes, specify that version: 5.8.6
Activity
santiagodoldan commentedon May 22, 2019
Looking at the code (https://github.com/sequelize/sequelize/blob/master/lib/associations/belongs-to.js and https://github.com/sequelize/sequelize/blob/master/lib/associations/has-many.js), there you can see that
sourceKeydoes not work with belongs to associations andtargetKeydoes not work with has many associations. I think that's the main difference.aprilmintacpineda commentedon May 22, 2019
Still they function the same way, so why not just use the same name for both?
santiagodoldan commentedon May 22, 2019
I don't think they do the same thing, they do the opposite indeed.
When you use
sourceKeyin a has many or has one association, you are referring to a column of the current model, on the other hand when you usetargetKeyin a belong to association, there you are referring to a column of the target model, not the current one, if that makes sense heh.I think the problem with your samples is that you are passing
targetKeyto hasOne/hasMany associations and if you take a look at the implementation you will see that it won't be used at all, so it would be the same as if you passcustomTargetKey, that should give you the same result.I'm pretty sure that Sequelize is smart enough to get the correct column without the need of passing
sourceKeyand I'm sayingsourceKeybecause that's the correct option for hasOne/hasMany associations, I'm referring to this snippet btwsantiagodoldan commentedon May 22, 2019
Also, try to check how
foreignKeyworks for belongsTo and hasMany, you will see that they work the opposite way, which makes sense.https://github.com/sequelize/sequelize/blob/master/lib/associations/has-many.js#L74-L77
https://github.com/sequelize/sequelize/blob/master/lib/associations/belongs-to.js#L50-L53