Skip to content

Bug with include query on many to many relationship #4222

Closed

Description

@thisjunewest

When you make a query on a table with a many to many relationship (i.e. belongs to many through), including the related model, using attributes: [] causes an error.

We were using this feature to make queries based on conditions in the related model and previously this would prevent the related model from being hydrated.

Introduced between 3.0.0. and 3.1.1

Test case for the bug here:

sequelize.define('Cat', { name: Sequelize.STRING });
sequelize.define('Food', { title: Sequelize.STRING });
sequelize.define('CatFood', { type: Sequelize.STRING });

var db = sequelize.models;

db.Cat.belongsToMany(db.Food, { as: 'meals', foreignKey: { name: 'catId' }, through: 'CatFood' });

db.Cat.sync()
  .then(function() { return db.Food.sync(); })
  .then(function() { return db.CatFood.sync(); })
  .then(function() {
    return db.Food.create({ title: 'Miaow Mix' })
  })
  .then(function(food) {
    return db.Cat.create({ name: 'Fluffy' })
    .then(function(cat) {
      return cat.addMeal(food, { type: 'snack' });
    });
  })
  .then(function() {
    db.Cat.findAll({
      include: {
        model: db.Food,
        as: 'meals',
        attributes: []
      }
    });
  }).then(function(cats) {
    console.log(cats);
  });

Error is:

TypeError: Cannot set property 'CatFood' of undefined
    at groupJoinData (/.../node_modules/sequelize/lib/dialects/abstract/query.js:329:37)
    at AbstractQuery.handleSelectQuery (/.../node_modules/sequelize/lib/dialects/abstract/query.js:537:15)
    at Statement.<anonymous> (/.../node_modules/sequelize/lib/dialects/sqlite/query.js:93:33)

Activity

  1. janmeier commented on Jul 30, 2015

    @janmeier
    Member

    Hmm, yeah I guess that if include.attributes is [], so should include.through.attributes

  2. added
    type: bug
    on Jul 30, 2015
  3. markdboyd commented on Jul 13, 2016

    @markdboyd

    I'm experiencing this bug as well. And I think this bug is the same one as #5078 and #5958. Would you be able to point me towards which part of the code you think is causing the problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions