Repository navigation
Description
Sometimes models have relationships between them which rely on intermediate models.
Here's an example:
Data about users is split into two models User and Profile (with User containing core authentication info, and Profile containing more general public info about the user) and then a Profile can have several Email records associated with it.
var User = sequelize.define('user', {
username: Sequelize.STRING(50),
password: Sequelize.STRING(50)
} );
var Profile = sequelize.define('profile', {
firstName: Sequelize.STRING(50),
lastName: Sequelize.STRING(50)
} );
var Email = sequelize.define('email', {
email: {
type: Sequelize.STRING(200),
unique: true
}
} );
User.hasOne(Profile);
Profile.belongsTo(User);
Profile.hasMany(Email);
Email.belongsTo(Profile);Now, if you want to get all the Emails for a User, you have to go through Profile:
User.find( {
where: {id: 123},
include: {
model: Profile,
include: Email
}
} ).then( function(user) {
var emails = user.profile.emails;
// do something with emails
} );It would be really useful if you could define the indirect association on the model with something like:
User.hasMany(Email, {
through: {
model: Profile,
include: Email
}
} );
Email.belongsTo(User, {
through: {
model: Profile,
include: User
}
} );And then just do:
User.find( {
where: {id: 123},
include: Email
} ).then( function(user) {
var emails = user.emails;
// do something with emails
} );or:
user.getEmails().then( function(emails) {
// do something with emails
} );This should also work for very complex associations through multiple join tables.
The intention is to:
- Define relationships between models in the model definitions rather than in code which fetches those associations - which in my opinion is where they belong
- Remove boiler-plate code every time you want to
find()that association - Therefore make code more maintainable as you can switch around the underlying implementation of an association (e.g. changing the
ProfiletoEmailrelationship to many-to-many) without having to change the code everywhere you fetch that association
My use case is that I'm building apps with a lot of complex nested relationships between models, and constantly find myself writing the same find({ include: ... }) code over and over again. It would be great to be able to cut that down.
Does anyone have any thoughts on this?
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
mickhansen commentedon Jun 15, 2015
That's actually a feature request that pops up now and again (some even expecting non-N:M relations to support through).
However i might suggest using an association reference as the through argument:
But i suppose we could trivially support both :)
overlookmotel commentedon Jun 15, 2015
@mickhansen Do you think this would be hard to implement? I don't know the innards of how
throughis dealt with inside Sequelize.Yes, supporting associations as the value of
throughcould be very nice, as well as anincludesyntax.mickhansen commentedon Jun 16, 2015
We only support
throughfor N:M.through for 1:1 or 1:M would work differently - I don't think it would be too tricky though.
overlookmotel commentedon Jun 16, 2015
I was thinking it would be great if it also supported nested relationships.
Like: A
Tourhas manyConcerts, aConcerthas manyBands, aBandhas manyMusicians.Then you could do:
This kind of case is where the feature would really come into it's own, as it would cut out a huge amount of
model.find( { include: ... } )logic.Would that make it much more difficult to implement?
overlookmotel commentedon Jun 16, 2015
Or using associations, as you suggested:
mickhansen commentedon Jun 16, 2015
Hmm, i suppose you'd just need some recursive logic to build the join. However the syntax for defining a nested through model is not that clear imo.
overlookmotel commentedon Jun 17, 2015
@mickhansen Do you have another idea for the nested through model syntax?
Maybe it'd be best only to support the association syntax, to keep it simple.
overlookmotel commentedon Jun 13, 2016
Related issue: #3845
stale commentedon Jun 29, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
overlookmotel commentedon Jun 29, 2017
I'm still keen on implementing this at some point.
JoshuaWise commentedon Aug 14, 2017
Is this issue being looked at? I'm trying to implement an indirect relationship but sequelize won't let me express my database design. It's too limited without indirect relationships
mikefowler commentedon Oct 2, 2017
@mickhansen has any further thought been given to this? It seems that this is a pretty common type of relationship that those coming from Rails-land will expect to be possible. /cc @overlookmotel
andrew-st-angelo-77media commentedon Aug 22, 2018
I agree that this would be very useful. I use the same indirect/pass-through association in Rails, frequently. Further, if what you're interested is data associated with the secondary model, if would save bandwidth on a query to not have to pull down the intermediate model, as part of a
Primary.find({ ... , include: { model: NonTrivialIntermediate, include: NeededModels }})stale commentedon Jul 23, 2019
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂