Skip to content

targetKey for hasMany and hasOne #4258

Closed

Description

@rm-richard

I have two legacy tables in a 1:m association:
Property(id, statistical_id, name, ...), VisitorStats(id, statistical_id, total, ...)

The following works:

VisitorStats.belongsTo(Property, { foreignKey: 'statistical_id', targetKey: 'statistical_id'} );

Then, in the controller, I can query for VisitorStats and eager-load the Property for it.

However, I want to query the Property table and include the VisitorStats model. For this, I would have to do a hasMany association, like this:

Property.hasMany(VisitorStats, { foreignKey: 'statistical_id' });

hasMany's doesn't support the targetKey parameter option, and automatically joins VisitorStats.statistical_id on Property.id when I query. I expected the targetKey property to work like at a belongsTo association.

Is it possible to change the target key using hasMany? If not, how should I represent this 1:m association?

Activity

  1. added
    type: feature
    on Aug 5, 2015
  2. janmeier commented on Aug 5, 2015

    @janmeier
    Member

    targetKey is not yet supported for hasMany

  3. changed the title [-]hasMany association always joins on primary key[/-] [+]targetKey for hasMany and hasOne[/+] on Aug 6, 2015
  4. treejanitor commented on Aug 29, 2015

    @treejanitor

    This seems like a very useful modeling feature, especially for legacy databases.

  5. sijmenvos commented on Aug 31, 2015

    @sijmenvos

    I agree +1

  6. treejanitor commented on Aug 31, 2015

    @treejanitor

    I ended up having to use a raw SQL query, otherwise I would have had to invert the true object model to make the query work. So, it's possible to get the job done, but my premise is that an ORM ideally models logically the object tree as it should exist.

  7. PyroSA commented on Sep 7, 2015

    @PyroSA

    As per #4269, it is not working for HasOne/BelongsTo relationships either.
    The ForeignKey works, but it still uses .id for the include statement.

  8. mickhansen commented on Sep 7, 2015

    @mickhansen
    Contributor

    @PyroSA BelongsTo is the only one it works for, are you saying it no longer works, or are you saying it doesn't work from the HasOne side (which it never has)?

  9. PyroSA commented on Sep 7, 2015

    @PyroSA

    It does not work for the BelongsTo in our case either.

    Not my project, but off the top of my head it would look like:

    Model: Ta:
    columns[id, guid, someStuff]
    associate: Ta.hasOne Tb, {foreignKey: 'guid'}
    
    Model: Tb:
    columns[id, guid, otherStuff]
    associate: Tb.belongsTo Ta, {foreignKey: 'guid', targetKey: 'guid'}
    

    Not sure of the order (Ta/Tb) but:
    Tb.findAll include: [Ta]
    then tries to join on Ta.id == Tb.guid instead of Ta.guid == Tb.guid

    I can confirm the exact sequence if it's unclear.
    guids are stored as strings in this case.

  10. PyroSA commented on Sep 7, 2015

    @PyroSA

    @mickhansen - Tried to clarify above

  11. mickhansen commented on Sep 7, 2015

    @mickhansen
    Contributor

    @PyroSA We have a test for that feature, so it should work - Please open a seperate issue with a reproduceable case since this issue deals with expanding the feature.

  12. PyroSA commented on Sep 7, 2015

    @PyroSA

    @mickhansen - I brought it up because #4269 was closed and referred to this feature. If it's considered a separate issue, can't that issue be re-opened?

    If not I can create a new issue.

  13. guigrpa commented on Sep 8, 2015

    @guigrpa

    I have opened a new issue, as @mickhansen suggested: #4455

  14. mickhansen commented on Sep 8, 2015

    @mickhansen
    Contributor

    @PyroSA that issue is about HasOne, not Belongsto :)

  15. colegleason commented on Sep 16, 2015

    @colegleason

    Hey, I also have legacy tables and would love this feature. Is there any good workaround in the meantime?

  16. 19 remaining items

  17. LoneWolfPR commented on Jan 23, 2017

    @LoneWolfPR
    Contributor

    Is there any chance of this getting addressed anytime soon. We have a project using Heroku Connect with Salesforce. Salesforce tables join using the 'sfid' field which is not an integer primary key. I really need to be able specify a 1 to many relationship with hasMany setting the targetKey to this field instead of the primary key. Just seems strange to me that this has been sitting out so long.

  18. janmeier commented on Jan 23, 2017

    @janmeier
    Member

    @LoneWolfPR it's been sitting so long because no one has taken the time to finish the code and tests.

    As Felix mentions, the code is actually there, some one just needs to pick it up and add some tests

  19. LoneWolfPR commented on Jan 23, 2017

    @LoneWolfPR
    Contributor

    @janmeier Thanks, I guess I missed that. That said this PR seems to address all the issues. Is it possible to just get the conflict in the changelog fixed so this can get merged? #6652

  20. joe-angell commented on May 11, 2017

    @joe-angell

    How about sourceKey for hasOne?

  21. mlyticsAdamGe commented on Feb 2, 2018

    @mlyticsAdamGe

    To describe the relationship
    belongsTo --->use targetKey to set the foreigner key from
    hasMany --->use sourceKey to set the foreigner key from

    or it will bind to the primary key

  22. philipimperato commented on Apr 3, 2018

    @philipimperato

    Is Sequelize still being actively developed? There a bunch of issues that have are stale.

  23. eggerand13 commented on Nov 28, 2018

    @eggerand13

    Maybe this information should be added to the legacy database handling in the documentation (http://docs.sequelizejs.com/manual/advanced/legacy.html)

    Do I understand correctly that a fix for v4 has been commited but not been merged? mkaufmaner@1879d39

  24. williamkwao commented on May 21, 2019

    @williamkwao
    Country.hasMany(City, {foreignKey: 'countryCode', sourceKey: 'isoCode'});
    City.belongsTo(Country, {foreignKey: 'countryCode', targetKey: 'isoCode'});

    This example from the pr worked for me. I wish the documentation would be updated correctly. It took a while to figure it out.

  25. piotrgulbis commented on Jun 29, 2019

    @piotrgulbis
    Country.hasMany(City, {foreignKey: 'countryCode', sourceKey: 'isoCode'});
    City.belongsTo(Country, {foreignKey: 'countryCode', targetKey: 'isoCode'});

    This example from the pr worked for me. I wish the documentation would be updated correctly. It took a while to figure it out.

    This fixed the issue for me as well. Would be very helpful to update the docs!

  26. dilipsuthar97 commented on Apr 13, 2021

    @dilipsuthar97
    Country.hasMany(City, {foreignKey: 'countryCode', sourceKey: 'isoCode'});
    City.belongsTo(Country, {foreignKey: 'countryCode', targetKey: 'isoCode'});

    This example from the pr worked for me. I wish the documentation would be updated correctly. It took a while to figure it out.

    Thanks @williamkwao, You saved my day

  27. cpatti97100 commented on Apr 13, 2021

    @cpatti97100

    please someone who tried this out update the docs :) that would be really helpful for the future!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions