belongsToMany eager loading, unexpected error
See original GitHub issueHi, I have the following models:
user.js:
var User = sequelize.define('User', {
uuid: { type: types.UUID, primaryKey: true },
email: { type: types.STRING, allowNull: false, unique: true },
googleId: { type: types.STRING, allowNull: false, unique: true }
},
{
classMethods: {
associate: function (models) {
User.belongsToMany(models.Account, {
as: 'SharedAccounts',
through: models.AccountParticipant,
foreignKey: 'userUuid'
});
User.hasMany(models.Account, {
as: 'Accounts',
foreignKey: 'ownerUuid'
});
}
}
});
account.js:
var Account = sequelize.define('Account', {
uuid: { type: types.UUID, primaryKey: true },
name: { type: types.STRING, allowNull: false },
deleted: { type: types.BOOLEAN, defaultValue: false },
version: types.INTEGER
},
{
classMethods: {
associate: function (models) {
Account.belongsToMany(models.User, {
as: 'Participants',
through: models.AccountParticipant,
foreignKey: 'accountUuid'
});
Account.belongsTo(models.User, {
as: 'Owner',
foreignKey: 'ownerUuid'
});
}
}
});
And account-participant`:
var AccountParticipant = sequelize.define('AccountParticipant', {
deleted: { type: types.BOOLEAN, defaultValue: false },
version: types.INTEGER
});
The thing is, when I try to include AccountParticipant
in a query
Account.findAll({ include: [{ model: AccountParticipant, as: 'Participants' }] });
I get the following error:
Unhandled rejection Error: AccountParticipant (Participants) is not associated to Account!
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Ask Question
Laravel eager loading error "Trying to get property of non-object in .../BelongsToMany.php" ; App\BaseModel · Illuminate · Database ; App\Review
Read more >Eager loading not working on many to many relationship
Set up; I have a Programme model with a one to one relation to a User (belongsTo) many to many relation to a...
Read more >[Solved]-Eager Loading with Conditional has_many results in ...
[Solved]-Eager Loading with Conditional has_many results in Unknown Column Error-ruby ... I was able to get it working by adding a references. ......
Read more >Eloquent Performance: 4 Examples of N+1 Query Problems
A big part of that is a so-called "N+1 Query Problem". ... When you use eager loading, Eloquent gets all the records into...
Read more >Search in a smart field (with polymorphic relation) - Help me!
Maybe your missing a where clause in a sub include to make your where on the Identities model and use the eager loading...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Account.findAll({ include: [{ model: AccountParticipant, as: 'Participants' }] });
should beAccount.findAll({ include: [{ model: User, as: 'Participants' }] });
since the target of the association is user.If you want to include the join model directly you’ll need to setup associations to that.
Thanks guys, didn’t knew about these
sequelize.where
andsequelize.col
functions. I really appreciate your help!