Skip to content

Separate: true — Cannot read property 'push' of undefined #7514

Closed

Description

@anstill
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

Activity

  1. xdroidx commented on May 24, 2017

    @xdroidx

    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.

  2. stale commented on Jul 23, 2017

    @stale

    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 🙂

  3. jrcrawfo commented on Jul 25, 2017

    @jrcrawfo

    Still an issue

  4. edwinvanrijkom commented on Aug 22, 2017

    @edwinvanrijkom

    Running into this as well (v.4.6.0).

  5. elhoyos commented on Aug 25, 2017

    @elhoyos
    Contributor

    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 [email protected] on postgres.

  6. zeing commented on Sep 19, 2017

    @zeing

    @elhoyos omg , you help me <3

  7. anstill commented on Sep 21, 2017

    @anstill
    Author

    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.
  8. antl3x commented on Oct 29, 2017

    @antl3x

    Using Sequelize 4.19.0 and this not solved.

  9. flamewow commented on Nov 14, 2017

    @flamewow

    got same issue

  10. narin commented on Nov 16, 2017

    @narin

    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.

  11. stale commented on Feb 14, 2018

    @stale

    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 🙂

  12. 1 remaining item

  13. added
    type: bug
    on Apr 7, 2018
  14. fareshan commented on May 15, 2018

    @fareshan

    this is not resolved yet

  15. sushantdhiman commented on May 15, 2018

    @sushantdhiman
    Contributor

    @fareshan You tried with which version?

  16. fareshan commented on May 15, 2018

    @fareshan
  17. sushantdhiman commented on May 15, 2018

    @sushantdhiman
    Contributor

    @fareshan It's fixed in latest v5 beta

  18. fareshan commented on May 15, 2018

    @fareshan

    @sushantdhiman ok thanks
    Is there a workaround for v4 ?
    I am using sequelize-typescript which still does not support v5 yet

  19. ebaynaud commented on Nov 16, 2018

    @ebaynaud

    no workaround available for v4?

  20. matamune94 commented on Jun 28, 2019

    @matamune94
    const Film_types = sequelize.define('film_types', {
      film_id: {type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true},
      slug: Sequelize.STRING,
    });
    
    const Episodes = sequelize.define('episodes', {
      ep_id: {type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true},
      film_id: Sequelize.BIGINT(20).UNSIGNED,
      stt: Sequelize.INTEGER,
      ep_num: Sequelize.STRING,
      ep_name: Sequelize.TEXT('tiny'),
      ep_des: Sequelize.TEXT,
      ep_thumb: Sequelize.TEXT('tiny'),
      ep_data: Sequelize.TEXT('long')
    });
    Films.hasMany(Episodes, {foreignKey: 'film_id'})
    Episodes.belongsTo(Films, {foreignKey: 'film_id'})
    
    Film_types.hasMany(Episodes, {foreignKey: 'film_id'})
    Episodes.belongsTo(Film_types, {foreignKey: 'film_id'})
    
    Films.findAll({ include: [{ model: Film_types, include: [{ model: Episodes, attributes:['stt','ep_num'], limit: 1, order:[ ['stt','DESC'] ], where:{ ep_num: { [Op.notLike]: '%OVA%' } }, separate: true }], where: { slug: items } }], attributes, limit, order:[ ['film_id','DESC'] ], where, raw: false})
    

    Hi @sushantdhiman ,I make error Cannot read property 'push' of undefined, maybe can help ?

  21. tiagoravache commented on Aug 27, 2019

    @tiagoravache

    Same error sequelize version 5.8.5, and the suggest of @elhoyos doesn't work here...any other suggestion?

  22. papb commented on Aug 27, 2019

    @papb
    Member

    @matamune94 @tiagoravache Please open a new issue (following the template) and linking to this issue as a reference.

  23. kanwarmanish commented on Jan 1, 2023

    @kanwarmanish

    sequelize: 5.21.2 - still an issue. Could any body resolve it?

  24. caseyt commented on Dec 9, 2024

    @caseyt

    Still an issue as of v 7.0. This repo is terrible about closing issues that still exist.

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