Skip to content

belongsToMany.add returns nested array of array #8377

Closed

Description

@neves

Sequelize Version 4.11.0 through 4.11.7 and master branch

Look for console.log(userProject)

const Sequelize = require('sequelize')

var sequelize = new Sequelize('lab', 'root', 'root', {
  host: '127.0.0.1',
  dialect: 'mysql',
  define: {
    timestamps: false,
    constraints: false
  }
})

const User = sequelize.define('User')
const Project = sequelize.define('Project')
const UserProject = sequelize.define('UserProject')
const Task = sequelize.define('Task')
User.belongsToMany(Project, {through: 'UserProject'})
Project.hasMany(Task)
exec()

async function exec () {
  await sequelize.sync({force: true})

  var user = await User.create()
  var project = await Project.create()
  var userProject = await user.addProject(project)
  console.log(userProject) // wrongly returns a array of array, instead of just UserProject
  // [ [ UserProject {
  //   dataValues: [Object],
  //   _previousDataValues: [Object],
  //   _changed: [Object],
  //   _modelOptions: [Object],
  //   _options: [Object],
  //   __eagerlyLoadedAssociations: [],
  //   isNewRecord: false } ] ]
  
  userProject = await user.createProject() // correctly returns a project
  console.log(userProject.toJSON()) // { id: 2 }

  var task = await project.createTask()
  console.log(task.toJSON()) // { id: 1, ProjectId: 1 }

  return sequelize.close()
}

What do you expect to happen?

The output from console.log(userProject) should be

UserProject {
    dataValues: [Object],
    _previousDataValues: [Object],
    _changed: [Object],
    _modelOptions: [Object],
    _options: [Object],
    __eagerlyLoadedAssociations: [],
    isNewRecord: false }

What is actually happening?

But the output is a array of array of UserProject:

[ [ UserProject {
    dataValues: [Object],
    _previousDataValues: [Object],
    _changed: [Object],
    _modelOptions: [Object],
    _options: [Object],
    __eagerlyLoadedAssociations: [],
    isNewRecord: false } ] ]

If this behaviour is intentional, I would like to know why, since user.createProject or project.createTask works as expected.

Activity

  1. sushantdhiman commented on Sep 30, 2017

    @sushantdhiman
    Contributor

    I dont know what Sequelize version you are using. Please follow issue template properly.

  2. neves commented on Sep 30, 2017

    @neves
    Author

    @sushantdhiman It's version 4.11.0

  3. sushantdhiman commented on Sep 30, 2017

    @sushantdhiman
    Contributor

    @neves This is how API is designed, add expects single / multiple instances to add. To keep API consistent for both cases we return an array

  4. neves commented on Sep 30, 2017

    @neves
    Author

    @sushantdhiman, there's no difference between addProject and addProjects?
    So, if I need to get the created project, I should do:

    var userProjects = await user.addProject(project)
    var userProject = userProjects[0][0]

    Is this by design?

  5. sushantdhiman commented on Oct 1, 2017

    @sushantdhiman
    Contributor

    It should have returned an array of instances, rather than array of array of instances. Are you 100% sure its not an array of instances?

  6. neves commented on Oct 1, 2017

    @neves
    Author

    Reproducible code

    // https://github.com/sequelize/sequelize/issues/8377
    // yarn add sequelize sqlite3
    const Sequelize = require('sequelize')
    
    var sequelize = new Sequelize('sqlite://:memory:', {
      define: {
        timestamps: false,
        constraints: false
      }
    })
    
    const User = sequelize.define('User')
    const Project = sequelize.define('Project')
    const UserProject = sequelize.define('UserProject')
    const Task = sequelize.define('Task')
    User.belongsToMany(Project, {through: 'UserProject'})
    Project.hasMany(Task)
    exec()
    
    async function exec () {
      await sequelize.sync({force: true})
    
      var user = await User.create()
      var project = await Project.create()
      var userProject = await user.addProject(project)
      console.log(userProject) // wrongly returns a array of array, instead of just UserProject
      // [ [ UserProject {
      //   dataValues: [Object],
      //   _previousDataValues: [Object],
      //   _changed: [Object],
      //   _modelOptions: [Object],
      //   _options: [Object],
      //   __eagerlyLoadedAssociations: [],
      //   isNewRecord: false } ] ]
    
      userProject = await user.createProject() // correctly returns a project
      console.log(userProject.toJSON()) // { id: 2 }
    
      var task = await project.createTask() // correctly returns a task
      console.log(task.toJSON()) // { id: 1, ProjectId: 1 }
    
      return sequelize.close()
    }
  7. stale commented on Dec 30, 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 🙂

  8. neves commented on Jan 2, 2018

    @neves
    Author

    Does this behaviour is intentional?

  9. stale commented on Apr 2, 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 🙂

  10. added
    type: bug
    and removed on Apr 2, 2018
  11. sushantdhiman commented on Apr 10, 2018

    @sushantdhiman
    Contributor

    Just tested this, I confirm this odd behaviour

  12. sushantdhiman commented on Jul 22, 2018

    @sushantdhiman
    Contributor

    Fixed #9700

  13. antonioidalecio commented on Feb 26, 2019

    @antonioidalecio

    Reproducible code

    // https://github.com/sequelize/sequelize/issues/8377
    // yarn add sequelize sqlite3
    const Sequelize = require('sequelize')
    
    var sequelize = new Sequelize('sqlite://:memory:', {
      define: {
        timestamps: false,
        constraints: false
      }
    })
    
    const User = sequelize.define('User')
    const Project = sequelize.define('Project')
    const UserProject = sequelize.define('UserProject')
    const Task = sequelize.define('Task')
    User.belongsToMany(Project, {through: 'UserProject'})
    Project.hasMany(Task)
    exec()
    
    async function exec () {
      await sequelize.sync({force: true})
    
      var user = await User.create()
      var project = await Project.create()
      var userProject = await user.addProject(project)
      console.log(userProject) // wrongly returns a array of array, instead of just UserProject
      // [ [ UserProject {
      //   dataValues: [Object],
      //   _previousDataValues: [Object],
      //   _changed: [Object],
      //   _modelOptions: [Object],
      //   _options: [Object],
      //   __eagerlyLoadedAssociations: [],
      //   isNewRecord: false } ] ]
    
      userProject = await user.createProject() // correctly returns a project
      console.log(userProject.toJSON()) // { id: 2 }
    
      var task = await project.createTask() // correctly returns a task
      console.log(task.toJSON()) // { id: 1, ProjectId: 1 }
    
      return sequelize.close()
    }

    I'm currently facing this issue! I'm using sequelize version "^4.42.0"

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