Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including only pivot table #11230

Open
n-sviridenko opened this issue Jul 26, 2019 · 2 comments
Open

Including only pivot table #11230

n-sviridenko opened this issue Jul 26, 2019 · 2 comments
Labels
question status: understood For issues. Applied when the issue is understood / reproducible. type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference.

Comments

@n-sviridenko
Copy link

n-sviridenko commented Jul 26, 2019

Hi, we have a many-to-many relation b/w Foo and Bar models:

  const Foo = sequelize.define('foo', {
}, {
    tableName: 'foo',
  });
  Foo.associate = (db) => {
    Foo.Bars = Foo.belongsToMany(db.bar, { 
      as: 'bars',
      through: db.fooBar,
      foreignKey: 'fooId'
    });
  };
  const Bar = sequelize.define('bar', {
}, {
    tableName: 'bar',
  });
const FooBar = sequelize.define('fooBar', {}, {
    tableName: 'foo_bar',
  });

In order to save on performance, we want to include only a pivot table (FooBar) when executing Foo.findAll() and filter by Bar.id. Something like:

    include: [
      { model: FooBar, as: 'fooBars' },
    ],

instead of

    include: [
      { association: Bar, as: 'bars' },
    ],

But we're having: 'SequelizeEagerLoadingError: fooBar is not associated to foo!\

We need the data which is available there and we don't need any data from the associated table. Is this possible to do so?

@papb
Copy link
Member

papb commented Jul 26, 2019

In a way, yes, you have to do a 'trick' that I plan on documenting soon. Stay tuned!

@papb papb added dialect: all question status: understood For issues. Applied when the issue is understood / reproducible. type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference. labels Jul 26, 2019
@papb papb self-assigned this Jul 26, 2019
@papb papb removed the dialect: all label Sep 17, 2019
@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 8, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question status: understood For issues. Applied when the issue is understood / reproducible. type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference.
Projects
None yet
Development

No branches or pull requests

3 participants