targetKey for hasMany and hasOne
See original GitHub issueI 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:
- Created 8 years ago
- Reactions:18
- Comments:39 (12 by maintainers)
How about
sourceKey
forhasOne
?This example from the pr worked for me. I wish the documentation would be updated correctly. It took a while to figure it out.