sequelize: [bug?] Creating with associations

I want to create Company instance along with associated Address:

var Sequelize = require('sequelize')

var sequelize = new Sequelize('postgres://localhost/db', { logging: console.log })

var Company = sequelize.define('Company', {
  AddressId: {
    type: Sequelize.INTEGER,
    allowNull: false,
    unique: true
  }
})

var Address = sequelize.define('Address', {
  street: Sequelize.TEXT
})

Company.belongsTo(Address)

sequelize.sync({ force: true })
.then(function () {
  return Company.create({
    Address: { street: 'street1' }
  }, {
    include: [
      Address
    ]
  })
})

Output:

Unhandled rejection SequelizeValidationError: notNull Violation: AddressId cannot be null
    at /Users/alek/Desktop/pros/node_modules/sequelize/lib/instance-validator.js:72:14
    at tryCatcher (/Users/alek/Desktop/pros/node_modules/sequelize/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/Users/alek/Desktop/pros/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:507:31)
    at Promise._settlePromiseAt (/Users/alek/Desktop/pros/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:581:18)
    at Promise._settlePromiseAtPostResolution (/Users/alek/Desktop/pros/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:245:10)
    at Async._drainQueue (/Users/alek/Desktop/pros/node_modules/sequelize/node_modules/bluebird/js/main/async.js:128:12)
    at Async._drainQueues (/Users/alek/Desktop/pros/node_modules/sequelize/node_modules/bluebird/js/main/async.js:133:10)
    at Async.drainQueues (/Users/alek/Desktop/pros/node_modules/sequelize/node_modules/bluebird/js/main/async.js:15:14)
    at process._tickCallback (node.js:442:13)

Quick workaround:

var Sequelize = require('sequelize')

var sequelize = new Sequelize('postgres://localhost/db', { logging: console.log })

var Company = sequelize.define('Company', {
  AddressId: {
    type: Sequelize.INTEGER,
    allowNull: false,
    unique: true
  }
})

var Address = sequelize.define('Address', {
  street: Sequelize.TEXT
})

Company.belongsTo(Address)

sequelize.sync({ force: true })
.then(function () {
  return Company.create({
    AddressId: -1, // <---------------------------------------- Fake ID
    Address: { street: 'street1' }
  }, {
    include: [
      Address
    ]
  })
})

Output (works!):

Executing (default): INSERT INTO "Addresses" ("id","street","updatedAt","createdAt") VALUES (DEFAULT,'street1','2015-10-22 14:08:05.306 +00:00','2015-10-22 14:08:05.306 +00:00') RETURNING *;
Executing (default): INSERT INTO "Companies" ("id","AddressId","updatedAt","createdAt") VALUES (DEFAULT,1,'2015-10-22 14:08:05.312 +00:00','2015-10-22 14:08:05.312 +00:00') RETURNING *;

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 20 (7 by maintainers)

Most upvoted comments

Hi guys. I was having the same problem. I fix it by creating a attribute of the ForeignKey on the model and adding the “allowNull” there and removing the “allowNull” atribute of all my associations.

Before: ` const user = sequelize.define(‘user’, { idUser: { type: dataTypes.INTEGER, primaryKey: true, autoIncrement: true, field: ‘id_user’, }, name: { type: dataTypes.TEXT, field: ‘name’, }, });

const email = sequelize.define(‘user’, { idEmail: { type: dataTypes.INTEGER, primaryKey: true, autoIncrement: true, field: ‘id_email’, }, email: { type: dataTypes.TEXT, field: ‘email’, }, });

user.hasMany(email, { foreignKey: { field: ‘id_user’, allowNull: false, }, });

email.belongsTo(user, { foreignKey: { field: ‘id_user’, allowNull: false, }, }); `

after: ` const user = sequelize.define(‘user’, { idUser: { type: dataTypes.INTEGER, primaryKey: true, autoIncrement: true, field: ‘id_user’, }, name: { type: dataTypes.TEXT, field: ‘name’, }, });

const email = sequelize.define(‘user’, { idEmail: { type: dataTypes.INTEGER, primaryKey: true, autoIncrement: true, field: ‘id_email’, }, email: { type: dataTypes.TEXT, field: ‘email’, }, idUser: { type: dataTypes.INTEGER, field: ‘id_user’, allowNull: false, }, });

user.hasMany(email, { foreignKey: ‘id_user’ });

email.belongsTo(user, { foreignKey: ‘id_user’, }); `

Ps: I’m still testing because I own more than 40 tablelas, but the ones I tested worked without problems.

Any solution for this issue? =S

Got hit by this 😃

Maybe you are right but it still a bug in sequelize (