Repository navigation
Closed
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Description
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.
Metadata
Metadata
Assignees
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Activity
sushantdhiman commentedon Sep 30, 2017
I dont know what Sequelize version you are using. Please follow issue template properly.
neves commentedon Sep 30, 2017
@sushantdhiman It's version 4.11.0
sushantdhiman commentedon Sep 30, 2017
@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
neves commentedon Sep 30, 2017
@sushantdhiman, there's no difference between addProject and addProjects?
So, if I need to get the created project, I should do:
Is this by design?
sushantdhiman commentedon Oct 1, 2017
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?
neves commentedon Oct 1, 2017
Reproducible code
stale commentedon Dec 30, 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 🙂
neves commentedon Jan 2, 2018
Does this behaviour is intentional?
stale commentedon Apr 2, 2018
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 🙂
sushantdhiman commentedon Apr 10, 2018
Just tested this, I confirm this odd behaviour
sushantdhiman commentedon Jul 22, 2018
Fixed #9700
antonioidalecio commentedon Feb 26, 2019
I'm currently facing this issue! I'm using sequelize version "^4.42.0"