I am using Sequelize with Node + MySQL.
I have a model structure similar to this:
// models:
var Group, Issue, Invite;
// many Issues per Group
Group.hasMany(Issue);
Issue.belongsTo(Group);
// Groups can invite other Groups to work on their Issues
Issue.hasMany(Invite, {foreignKey: groupId});
Invite.belongsTo(Issue, {foreignKey: groupId});
Group.hasMany(Invite, {foreignKey: inviteeId});
Invite.belongsTo(Group, {foreignKey: inviteeId});
// given an Issue id, include all Invites + invited Groups (inviteeId) - But how?
var query = {
where: {id: ...},
include: ???
};
Issue.find(query).complete(function(err, issue) {
var invites = issue.invites;
var firstInvitedGroup = issue.invites[0].group;
// ...
});
Is this at all possible? What are possible work-arounds? Thank you!
Sequelize allows you to join a third table that's related to the second table by creating a nested include. For example, suppose you have three tables with the following relations: The Users table has one-to-many relation with the Invoices table. The Invoices table has many-to-one relations with the Users table.
Creating the standard relationships To create a One-To-One relationship, the hasOne and belongsTo associations are used together; To create a One-To-Many relationship, the hasMany and belongsTo associations are used together; To create a Many-To-Many relationship, two belongsToMany calls are used together.
Sequelize Docs: Nested Eager Loading
Example
Issue.find({
include: [
{
model: Invite,
include: [Group]
}
]
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With