question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

belongsToMany eager loading, unexpected error

See original GitHub issue

Hi, 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:closed
  • Created 8 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mickhansencommented, Sep 14, 2015

Account.findAll({ include: [{ model: AccountParticipant, as: 'Participants' }] }); should be Account.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.

0reactions
andrezzoidcommented, Sep 14, 2015

Thanks guys, didn’t knew about these sequelize.where and sequelize.col functions. I really appreciate your help!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found