You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
as: {singular, plural} not working with include: {all: true} #9928
I am specifying the singular and plural names of an alias. I then later try to include with the all: true option, but that results in a SequelizeDatabaseError of { name: 'SequelizeEagerLoadingError' }.
When I specify an as without explicit singular/plurals this issue does not arise.
Note: I think the issue is similar to what @raxod502 commented here.
letname='theAliasName'// Below works with include: { all: true }
through: db.define(name,{amount: Sequelize.FLOAT,unit: Sequelize.STRING,dataSource: Sequelize.STRING}),as: name+'s'})// Below DOES NOT work
through: db.define(name,{amount: Sequelize.FLOAT,unit: Sequelize.STRING,dataSource: Sequelize.STRING}),as: {singular: name,plural: name+'s'}})
A little update: I dug a little deeper and the { all: true } seems to trigger the following issue:
SequelizeEagerLoadingError: <model> is associated to <model> multiple times. To identify the correct association, you must use the 'as' keyword to specify the alias of the association you want to include.[1]
I see now where sequelize gets confused, but this is something the { all: true } should be able to handle. Is this a known issue?
[1] is me replacing the name of the model with a generic placeholder
Ok, so how I ended up solving this is manually adding an associations list to every model .
Illustrating the principle:
// Create an array on every modelmodel.customAssociationList=[]// Populate each array with the relationshipsmodel.customAssociationList.push({model: childModel,as: 'theAliasUsed'})
Then when calling findOne I used:
include: model.associationList
My conclusion remains that this is not expected behavior. Using include: [ { all: true } ] should return all child models with known aliases.
// Make connectionconstSeq=require('sequelize')constseq=newSeq('githubissue','postgres','',{dialect: 'postgres',host: 'localhost',logging: true})// Make some modelsconstindividual=seq.define('individual',{name: Seq.STRING})consthat=seq.define('hat',{color: Seq.STRING})constevent=seq.define('event',{type: Seq.STRING})// Set model connectionshat.belongsToMany(individual,{through: event,as: {singular: 'hatwornby',plural: 'hatwornbys'}})individual.belongsToMany(hat,{through: event,as: {singular: 'personwearinghat',plural: 'personwearinghats'}})// Sync databaseseq.sync({force: true})// Create a person and a hat.then(f=>Promise.all([individual.create({name: 'Sir Derp'}),hat.create({color: 'Gold'})]))// Set the relationship.then(items=>{const[theperson,thehat]=itemsreturnthehat.addHatwornby(theperson)})// Find an individual and include his hats.then(f=>individual.findOne({where: {id: 1},include: [{model: hat,as: 'personwearinghats'}]}))// Console out the person with included hats: THIS WORKS.then(person=>console.log(person.toJSON()))// Now try to eager load the individual with all associations: THIS DOWS NOT WORK.then(f=>individual.findOne({where: {id: 1},include: [{all: true}]})).then(console.log.bind(console)).catch(console.log.bind(console))
I saw these updates and decided to verify with my above SSCCE. It still errors:
{id: 1,name: 'Sir Derp',createdAt: 2018-11-22T10:59:44.954Z,updatedAt: 2018-11-22T10:59:44.954Z,personwearinghats:
[{id: 1,color: 'Gold',createdAt: 2018-11-22T10:59:44.955Z,updatedAt: 2018-11-22T10:59:44.955Z,event: [Object]}]}{ SequelizeEagerLoadingError: hatisassociatedtoindividual using analias.You've included an alias ([object Object]), but it does not match the alias defined in your association.
atFunction._getIncludedAssociation(/Users/mentor/dev/sequelize-issue/node_modules/sequelize/lib/model.js:588:17)atFunction._validateIncludedElement(/Users/mentor/dev/sequelize-issue/node_modules/sequelize/lib/model.js:499:53)atoptions.include.options.include.map.include(/Users/mentor/dev/sequelize-issue/node_modules/sequelize/lib/model.js:395:37)atArray.map(<anonymous>)
at Function._validateIncludedElements (/Users/mentor/dev/sequelize-issue/node_modules/sequelize/lib/model.js:390:39)
at Promise.try.then.then (/Users/mentor/dev/sequelize-issue/node_modules/sequelize/lib/model.js:1570:14)
at tryCatcher (/Users/mentor/dev/sequelize-issue/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/mentor/dev/sequelize-issue/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/mentor/dev/sequelize-issue/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/mentor/dev/sequelize-issue/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/mentor/dev/sequelize-issue/node_modules/bluebird/js/release/promise.js:694:18)
at _drainQueueStep (/Users/mentor/dev/sequelize-issue/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/Users/mentor/dev/sequelize-issue/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/Users/mentor/dev/sequelize-issue/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/mentor/dev/sequelize-issue/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5) name: 'SequelizeEagerLoadingError' }
Activity
actuallymentor commentedon Sep 17, 2018
A little update: I dug a little deeper and the { all: true } seems to trigger the following issue:
SequelizeEagerLoadingError: <model> is associated to <model> multiple times. To identify the correct association, you must use the 'as' keyword to specify the alias of the association you want to include.[1]I see now where sequelize gets confused, but this is something the { all: true } should be able to handle. Is this a known issue?
[1] is me replacing the name of the model with a generic placeholder
actuallymentor commentedon Sep 18, 2018
Ok, so how I ended up solving this is manually adding an associations list to every model .
Illustrating the principle:
Then when calling findOne I used:
My conclusion remains that this is not expected behavior. Using
include: [ { all: true } ]should return all child models with known aliases.sushantdhiman commentedon Sep 25, 2018
Can I get a SSCCE which I can execute directly. You explanation is bit rough
( What is SSCCE ? You can find template for Sequelize here )
A few good example of SSCCE
removeAttribute('id')results inundefined: nulldata value #7318 (comment)actuallymentor commentedon Sep 25, 2018
Sure thing:
tsasaki609 commentedon Nov 10, 2018
Hello,
I checked the code. It seems that it is not difficult problem and I try to fix now.
Please wait for a while. 😸
fix(association): enable eager load with include all(sequelize#9928)
fix(association): enable eager load with include all(sequelize#9928)
fix(association): enable eager load with include all(sequelize#9928)
fix(association): enable eager load with include all(sequelize#9928)
fix(association): enable eager load with include all(sequelize#9928)
fix(association): enable eager load with include all(sequelize#9928)
fix(association): enable eager load with include all(#9928) (#10173)
actuallymentor commentedon Nov 22, 2018
Hey!
I saw these updates and decided to verify with my above SSCCE. It still errors:
Is this a known issue?
tsasaki609 commentedon Nov 23, 2018
I guess it has not been released yet.
@sushantdhiman When will it be?
sushantdhiman commentedon Nov 26, 2018
Will try releasing this weekend