You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found some posts about composite keys and relations but nothing is in the documentation and it doesn't seem to be supported (no relevant test cases)
Is this supported: Entity.hasMany(Other , {foreignKey: ['key1', ' key2', ' key3']});
Thanks,
Serge
Reacted by sl-asdrubalsuarez, Stan Reshetnyk, Hannes Tiede, Michael Bayne, CharlesJQuarra, Jose Veiga, Tim Schubert, Vladislav Kokovin, Aaron, jonathan and 126 more
Reacted by stefanonepa, Eduardo Hattori, Kyle Moore, Ocean, Ian Butler, Oleg Ozimok, Carlos Eduardo Scheffer, Jacob Pratt, Oleh Kachynskyi, Johnil Quezada and 75 more
Reacted by J.D Nicholls, armando magalhaes, Kyle Moore, Ocean, Ian Butler, Oleg Ozimok, Kate Lyzohubova, Eliza Zhang, Max Jacobson, Johnil Quezada and 29 more
Without it I guess I have to implement some instanceMethods flavour, right?
The custom get method would use attributes of the object to fetch associated entities.
Another suggestion before I start doing so?
Reacted by Ian Butler, Oleg Ozimok, Miguel Coronel, Jacob Pratt, Max Jacobson, W.T. Chang, Sungwoo Ryan Hwang, Denny Harijanto, Maples7, Sylvain Estevez and 51 more
Reacted by Leo Doldan, dlarr, Naman Kumar, Paola Alapizco, Carlos Cárcamo, Pedro Silveira Lopes, Adam Walker, Sergio Shevtsov, PatrickTchoupe, Jonatas Sousa Monteiro and 7 more
The innofluence fork is me and mickhansen. But the support is not yet complete, and we have agree not to fully implement it sequelize fully supports non-id primary keys. That is sometime in 1.7
Any updates on this issue yet? I currently need support for composite foreign keys. I guess, the best alternative is to simply impose a unique index on the composite key and use an auto-increment primary key instead?
In that case, let me share my index-creating code here for other people running into the same problem. I only tested this with MySQL. It might need some fixing for other SQL formats.
I just ran into this problem. Is there still no plan to support multi-column indexes on relations? Is there any workaround known other then using a single key index?
Specifically, I'm trying to use the "findAll" with an "include" filter.
Reacted by Brett Harris, Roberto Vaccaro, Nican, Eduen Sarceño, park ji hoon, Felipe Lube de Bragança, Sonic Xiang, Jhon Rocha, Rabah ABED, mauriciocl and 4 more
does anyone have this working for more complex queries? e.g. nested eager loading? in a case where ZoneDefinition is zoneGroup->zoneDefinitions because it's nested in an include?
@jayenashar have you found a solution for nested eager loading?
@jayenashar have you found a solution for nested eager loading?
i did but oh god it is so hacky. you may be better off making the full sql query manually.
Outcome.hasOne(OutcomeTime,{as: "first",foreignKey: "outcomeID",scope: {// this doesn't work for the nested query as the table has an alias// createdAt: Sequelize.col('Outcome.firstTime')createdAt: Sequelize.where(Sequelize.col("offers->outcomes.firstTime"),"=",Sequelize.col("offers->outcomes->first.createdAt")),},});
Damn, I made whole project using sequelize, everything is working fine, just one issue requires composite keys, now I am stuck.
Are you working with the latest version? There is v7, may be they added some logic for this use case. I know they added new features like transaction handling etc.
@ephys not sure if you looked into this before, but @papandreou is interested in this feature. What I could piece together from earlier comments here is that we want support for them in associations with this as proposed API (based on a comment of about a year ago, not sure if we updated associations already back then);
I don't think it would be that difficult, but it's also not a super simple PR. You'd basically need to:
Update the option normalization logic to turn foreignKey and targetKey into keys, and make sure both are not used at the same time
Expose a keys property
Rewrite the targetKey and foreignKey and other related fields (not options!) on association classes to make them retrieve the value from keys instead, and make them throw if keys.length > 1.
Support inferring keys from composite Primary Keys.
Update all places that use targetKey or foreignKey and other related fields in our codebase to make them use the new keys property instead.
Also, we could also consider naming keys -> foreignKeys and having the object keys be source and target:
source and target being context-sensitive. In belongsTo, source is the FK owner. In hasOne, source is the PK owner. That's already what happens with sourceKey and targetKey today: source is always the model on which the method was called, and target is always the parameter.
We also need an API design for belongsToMany:
ModelB.belongsToMany(ModelA, {
foreignKeys: [{
source: 'pk_1', // name of attribute on ModalB
target: 'pk_2', // name of attribute on ModalA
sourceFk: 'fk_1', // name of FK on through -> source, points at `source` key
targetFk: 'fk_2', // name of FK on through -> target, points at `target` key
}],
});
Names are not perfect but suggestions are welcome. We could have something more explicit like throughToSource and throughToTarget
Reacted by Andreas Lind, Abhishek Shah, Junyi, Nicolas Zulaica, Chris, Jonathan , Gibig, Steffen, Marc Hermann, buzite25 and 5 more
If you're trying to join models using composite keys with Sequelize's include, make sure to use the on attribute with literal() to define the custom join condition manually:
awaitModelA.findAll({include: {model: ModelB,on: literal(`"ModelA"."pk_1" = "ModelB"."pk_1" AND "ModelA"."pk_2" = "ModelB"."pk_2"`),},});
Activity
sdepold commentedon Nov 4, 2012
nope. i'm sorry :)
Zerzio commentedon Nov 4, 2012
I'm too ;-)
I found those:
Without it I guess I have to implement some instanceMethods flavour, right?
The custom get method would use attributes of the object to fetch associated entities.
Another suggestion before I start doing so?
pilsy commentedon Mar 2, 2013
Hi i was wondering if the code mentioned by Zerzio has been merged in yet? i couldn't see it anywhere
sdepold commentedon Mar 2, 2013
ahm. nope.
janmeier commentedon Mar 3, 2013
The innofluence fork is me and mickhansen. But the support is not yet complete, and we have agree not to fully implement it sequelize fully supports non-id primary keys. That is sometime in 1.7
Domiii commentedon Jul 23, 2014
Any updates on this issue yet? I currently need support for composite foreign keys. I guess, the best alternative is to simply impose a unique index on the composite key and use an auto-increment primary key instead?
mickhansen commentedon Jul 23, 2014
@Domiii No work has been done as of yet. That is your best alternative yes, personally i think that's the best solution always but that's just me.
Domiii commentedon Jul 23, 2014
In that case, let me share my index-creating code here for other people running into the same problem. I only tested this with MySQL. It might need some fixing for other SQL formats.
Example usage:
Code:
[-]hasMany using mutiple foreign keys[/-][+]Composite foreign keys[/+]kopertop commentedon Oct 1, 2015
I just ran into this problem. Is there still no plan to support multi-column indexes on relations? Is there any workaround known other then using a single key index?
Specifically, I'm trying to use the "findAll" with an "include" filter.
mickhansen commentedon Oct 1, 2015
@kopertop There's a difference between indexes and constraints, we support multi column indexes on all tables.
ivanjd commentedon Oct 5, 2015
That feature would be super helpful.
jamesaspence commentedon Oct 16, 2015
+1 I just ran into this as well, would make my job way easier. For now I guess I'm editing the table structure :/
ozum commentedon Dec 12, 2015
👍
mickhansen commentedon Dec 15, 2015
For everyone +1'ing this, are you looking to reference tables with multiple primary keys or what are the usecases here exacttly?
114 remaining items
ryanwaite28 commentedon Mar 1, 2023
Okay, so developers want something like this?
peralva commentedon Mar 9, 2023
It would be great
linokriza commentedon May 9, 2023
Any updates on this issue?
ephys commentedon Jun 21, 2023
I'd call the option
keysbut otherwise it does seem to be like it's the only possible/acceptable designrabahmilano commentedon Aug 10, 2023
does this syntaxe works with V6 ?
Skryabind commentedon Aug 20, 2023
@jayenashar have you found a solution for nested eager loading?
jayenashar commentedon Aug 20, 2023
i did but oh god it is so hacky. you may be better off making the full sql query manually.
KaKi87 commentedon Jan 7, 2024
I wonder, is there any other NodeJS ORM that has this feature ?
piotr-pawlowski commentedon Jan 9, 2024
TypeORM, but there are other annoying issues
sergioshev commentedon Jan 9, 2024
Are you working with the latest version? There is v7, may be they added some logic for this use case. I know they added new features like transaction handling etc.
WikiRik commentedon Jan 31, 2024
@ephys not sure if you looked into this before, but @papandreou is interested in this feature. What I could piece together from earlier comments here is that we want support for them in associations with this as proposed API (based on a comment of about a year ago, not sure if we updated associations already back then);
Do you have any idea how difficult this is to implement in v7?
ephys commentedon Feb 2, 2024
I don't think it would be that difficult, but it's also not a super simple PR. You'd basically need to:
foreignKeyandtargetKeyintokeys, and make sure both are not used at the same timekeyspropertytargetKeyandforeignKeyand other related fields (not options!) on association classes to make them retrieve the value fromkeysinstead, and make them throw ifkeys.length > 1.keysfrom composite Primary Keys.targetKeyorforeignKeyand other related fields in our codebase to make them use the newkeysproperty instead.Also, we could also consider naming
keys->foreignKeysand having the object keys besourceandtarget:source and target being context-sensitive. In
belongsTo, source is the FK owner. InhasOne, source is the PK owner. That's already what happens withsourceKeyandtargetKeytoday:sourceis always the model on which the method was called, andtargetis always the parameter.We also need an API design for
belongsToMany:Names are not perfect but suggestions are welcome. We could have something more explicit like
throughToSourceandthroughToTargetHarshil-Modi commentedon Apr 10, 2025
If you're trying to join models using composite keys with Sequelize's
include, make sure to use theonattribute withliteral()to define the custom join condition manually: