Skip to content

as: {singular, plural} not working with include: {all: true} #9928

Closed

Description

@actuallymentor

What are you doing?

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.

let name = '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'
	}
} )

Later these associations are called with:

Model.findOne( {
    where: { id: relevant_id, userId: user_id },
    include: [ { all: true } ]
} ) )

What do you expect to happen?

I expect:

  • The alias table to be populated when a relationship is set (happens in both cases, no issue)
  • The include: { all: true } to load the associated data

What is actually happening?

Sequelize throws { name: 'SequelizeEagerLoadingError' }

Dialect: postgres
Database version: 10.5
Sequelize version: 4.38.1
Tested with latest release: Yes

Edit 1: added code used to findOne
Edit 2: added error name

Activity

  1. actuallymentor commented on Sep 17, 2018

    @actuallymentor
    ContributorAuthor

    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

  2. actuallymentor commented on Sep 18, 2018

    @actuallymentor
    ContributorAuthor

    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 model
    model.customAssociationList = []
    
    // Populate each array with the relationships
    model.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.

  3. sushantdhiman commented on Sep 25, 2018

    @sushantdhiman
    Contributor
  4. actuallymentor commented on Sep 25, 2018

    @actuallymentor
    ContributorAuthor

    Sure thing:

    // Make connection
    const Seq = require ( 'sequelize' )
    const seq = new Seq( 'githubissue', 'postgres', '', {
    	dialect: 'postgres',
    	host: 'localhost',
    	logging: true
    } )
    
    // Make some models
    const individual = seq.define( 'individual', {
    	name: Seq.STRING
    } )
    
    const hat = seq.define( 'hat', {
    	color: Seq.STRING
    } )
    
    const event = seq.define( 'event', {
    	type: Seq.STRING
    } )
    
    // Set model connections
    hat.belongsToMany( individual, {
    	through: event,
    	as: {
    		singular: 'hatwornby',
    		plural: 'hatwornbys'
    	}
    } )
    
    individual.belongsToMany( hat, {
    	through: event,
    	as: {
    		singular: 'personwearinghat',
    		plural: 'personwearinghats'
    	}
    } )
    
    // Sync database
    seq.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 ] = items
    	return thehat.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 ) )
  5. added
    type: bug
    and removed on Oct 21, 2018
  6. tsasaki609 commented on Nov 10, 2018

    @tsasaki609
    Contributor

    Hello,
    I checked the code. It seems that it is not difficult problem and I try to fix now.
    Please wait for a while. 😸

  7. actuallymentor commented on Nov 22, 2018

    @actuallymentor
    ContributorAuthor

    Hey!

    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: hat is associated to individual using an alias. You've included an alias ([object Object]), but it does not match the alias defined in your association.
        at Function._getIncludedAssociation (/Users/mentor/dev/sequelize-issue/node_modules/sequelize/lib/model.js:588:17)
        at Function._validateIncludedElement (/Users/mentor/dev/sequelize-issue/node_modules/sequelize/lib/model.js:499:53)
        at options.include.options.include.map.include (/Users/mentor/dev/sequelize-issue/node_modules/sequelize/lib/model.js:395:37)
        at Array.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' }

    Is this a known issue?

  8. tsasaki609 commented on Nov 23, 2018

    @tsasaki609
    Contributor

    I guess it has not been released yet.

    @sushantdhiman When will it be?

  9. sushantdhiman commented on Nov 26, 2018

    @sushantdhiman
    Contributor

    Will try releasing this weekend

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