question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

targetKey for hasMany and hasOne

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:18
  • Comments:39 (12 by maintainers)

github_iconTop GitHub Comments

45reactions
joe-angellcommented, May 11, 2017

How about sourceKey for hasOne?

34reactions
williamkwaocommented, May 21, 2019
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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HasMany | @sequelize/core
Class HasMany<S, T, SourceKey, TargetKey, TargetPrimaryKey>. One-to-many association. See hasMany. Like with HasOne, the foreign key will be defined on the ...
Read more >
Sequelize targetKey not working - node.js - Stack Overflow
As of new version that i am using "sequelize": "^5.8.12" use sourceKey instead of targetKey for hasOne and hasMany relations works for me....
Read more >
Let's Do: Sequelize Associations - Josiah Mortenson
Relations are represented in code with "associations". These are the 4 types of associations, where A and B are sequelize Model s: A.hasOne...
Read more >
Associations - Manual | Sequelize
BelongsTo; HasOne; HasMany; BelongsToMany ... The target key is the column on the target model that the foreign key column on the source...
Read more >
Difference between has one belongs to and has many
The only difference between hasOne and belongsTo is where the foreign key column is located. Let's say you have two entities: User and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found