Skip to content

sourceKey and targetKey seems to be the same exact thing. #10969

Closed

Description

@aprilmintacpineda

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

  1. santiagodoldan commented on May 22, 2019

    @santiagodoldan

    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 sourceKey does not work with belongs to associations and targetKey does not work with has many associations. I think that's the main difference.

  2. aprilmintacpineda commented on May 22, 2019

    @aprilmintacpineda
    Author

    Still they function the same way, so why not just use the same name for both?

  3. santiagodoldan commented on May 22, 2019

    @santiagodoldan

    I don't think they do the same thing, they do the opposite indeed.

    When you use sourceKey in a has many or has one association, you are referring to a column of the current model, on the other hand when you use targetKey in 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 targetKey to 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 pass customTargetKey, 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 sourceKey and I'm saying sourceKey because that's the correct option for hasOne/hasMany associations, I'm referring to this snippet btw

    Users.hasOne(models.Bank, { foreignKey: 'id', targetKey: 'bank_id' });
    
  4. added
    docs
    on Jul 30, 2019
  5. self-assigned this
    on Jul 30, 2019
  6. added
    status: wip
    and removed on Oct 15, 2019
  7. removed
    status: wip
    on Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions