sequelize: Separate: true — Cannot read property 'push' of undefined

Model.hasMany(AnotherMmodelWithHasManyAssociations, { onDelete: 'CASCADE', as: 'test' });
Model.findAll({
        include: [
            {
                model: AnotherMmodelWithHasManyAssociations,
                as: 'test',
                separate: true,
            },
        ],
    })

After that Sequelize falls with error:

TypeError: Cannot read property 'push' of undefined
     at /usr/src/app/node_modules/sequelize/lib/associations/has-many.js:331:64
     at Array.forEach (native)
     at .<anonymous> (/usr/src/app/node_modules/sequelize/lib/associations/has-many.js:330:13)
 From previous event:
     at Promise.then (/usr/src/app/node_modules/sequelize/lib/promise.js:21:17)
     at HasMany.get (/usr/src/app/node_modules/sequelize/lib/associations/has-many.js:322:33)
     at /usr/src/app/node_modules/sequelize/lib/model.js:1442:32
 From previous event:
     at Function.map (/usr/src/app/node_modules/sequelize/lib/promise.js:21:17)
     at Function.Model.$findSeparate (/usr/src/app/node_modules/sequelize/lib/model.js:1420:18)
     at .<anonymous> (/usr/src/app/node_modules/sequelize/lib/model.js:1408:18)
 From previous event:
     at Promise.then (/usr/src/app/node_modules/sequelize/lib/promise.js:21:17)
     at Promise.all.then (/usr/src/app/src/modules/transaction/limits.service.js:139:10)

Dialect: mariaDb Database version: 10.1 Sequelize version: 3.30.4

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 17
  • Comments: 22 (4 by maintainers)

Most upvoted comments

I got to make it work on my side by specifying the foreignKey in the included attributes.

Foo.hasMany(Bar, { as: 'bars', foreignKey: 'special_id' });
Bar.belongsTo(Foo, { as: 'special', foreignKey: 'special_id' });

Foo
  .findAll({
    attributes: ['id', 'name'],
    where: {
      is_valid: true
    },
    order: [['name', 'DESC']],
    include: [{
      model: Bar,
      as: 'bars',
      attributes: ['special_id', 'name'], // error appears if special_id is not provided here
      where: {
        is_active: true
      },
      limit: 10, // causes the separate=true
      required: true
    }]
  })

I’m using sequelize@3.30.4 on postgres.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

For resolve this issue need:

  1. Explicitly indicate foreignKey:
Account.hasMany(AccountRoles, { onDelete: 'CASCADE', foreignKey: 'accountId', as: 'rolesIds' });
  1. Explicitly indicate this foreignKey to attributes.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

Having this issue as well. Including the foreign key in attributes as @elhoyos suggested did not solve the issue for me. Using 4.22.7.

Still an issue

Still an issue! @elhoyos thanks a lot!

no workaround available for v4?

I have just forced with same problem. In my case:

db.Outfits.hasMany(db.Likes, { foreignKey: 'outfit', as: 'likes' }); Outfits.findAll({ include: [ { model: Likes, attributes: [ 'user' ], as: 'likes', separate: true } ]}) So in my case same error appeared because of foreignKey ‘outfit’ lacking in attributes.

@andrew-polyansky So have you tried to add ‘foreignKey’ to associations? The problematic part of code looks like below. for (const instance of results) { result[instance.get(association.foreignKey, {raw: true})].push(instance); }

Anyway it’s good to have some warnings on this case or at least to document it somehow.