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

include: using options "separate" and "attributes" together throws TypeError: Cannot read property 'push' of undefined #8228

Closed
wolframkleis opened this issue Aug 30, 2017 · 6 comments

Comments

@wolframkleis
Copy link

What you are doing?

I try to include hasMany association with "include" that uses both "separate" option and "attributes" option

const Parent = sequelize.define('Parent',
  {	
    id:  		{ type: Sequelize.INTEGER, primaryKey: true},
    name:  		{ type: Sequelize.STRING(50)}
  });

const Child = sequelize.define('Child',
  {	
    id:  		{ type: Sequelize.INTEGER, primaryKey: true},
	name:  		{ type: Sequelize.STRING(50)}
  });

Parent.hasMany(Child,{as: 'children'});

sequelize.sync({force:true,logging: false})
.then(()=>Parent.create({
    id: 1, 
	name: 'Parent1',
	children: [{id: 1,name: 'Child1'},{id: 2,name: 'Child2'}]
  },{include: [ {association: 'children'} ]}))

//first, we use an include with only  "attributes"  option:  works fine
.then(()=>Parent.findAll({
  include: {
	association: 'children',
	attributes: ['name']
  }}))

//next we use an include with only "separate" option: works fine, too
.then(()=>Parent.findAll({
  include: {
	association: 'children',
	separate: true
  }}))

//finally, we try the include with both "separate" and "attributes"  options:  FAILS
.then(()=>Parent.findAll({
  include: {
	association: 'children',
	attributes: ['name'],
	separate: true
  }}))

What do you expect to happen?

I expect that you can use both options together.
It should be possible to select certain attributes only AND to request separate queries (to avoid the 63 character issue when using PostgreSQL, for example)

What is actually happening?

when using both options together, findAll fails with this error:

Unhandled rejection TypeError: Cannot read property 'push' of undefined
    at Model.findAll.then.results (C:\test\constraints\node_modules\Sequelize\lib\associations\has-many.js:230:66)
    at tryCatcher (C:\test\constraints\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\test\constraints\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (C:\test\constraints\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\test\constraints\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\test\constraints\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (C:\test\constraints\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (C:\test\constraints\node_modules\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (C:\test\constraints\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:651:20)
    at tryOnImmediate (timers.js:624:5)
    at processImmediate [as _immediateCallback] (timers.js:596:5)

Dialect: postgres
Database version: 9.4
Sequelize version: 4.7.5

@stale
Copy link

stale bot commented Sep 29, 2017

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 🙂

@stale stale bot added the stale label Sep 29, 2017
@stale stale bot closed this as completed Oct 6, 2017
@wolframkleis
Copy link
Author

wolframkleis commented Oct 6, 2017

not stale
The problem is still there.

@antl3x
Copy link

antl3x commented Oct 29, 2017

Facing the same problem!

@kgiannelos
Copy link

Same here, facing the same.

@sushantdhiman
Copy link
Contributor

Looks same as #7514, which has been fixed #9396 and available in 5.0.0-beta.5

@kgiannelos
Copy link

It looks the same indeed, but it could be different as well. In my case, adding the foreign keys to the model definition and also in the the attributes list in the include object did not help. However what worked out in my case was to remove completely the attributes list form the include. This worked even without declaring the foreign keys explicitly in the model definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants