question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug?] Creating with associations

See original GitHub issue

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 *;

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
Klaviccommented, Feb 6, 2019

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.

2reactions
calleufuzicommented, Aug 19, 2020

Any solution for this issue? =S

Read more comments on GitHub >

github_iconTop Results From Across the Web

Held keys in associations - bugs - Mathematica Stack Exchange
I think my answer here sheds some light on the situation after version 10.4.0. I believe the advice by WRI is "do not...
Read more >
Bug associations — Socorro documentation
The bugassociations Django command looks at all the bugs in Bugzilla that have been created or had their cf_crash_signature field updated for some...
Read more >
Error Creating an Association between Views in Entity ...
I have an EDMX with the views in the designer and am trying to manually create the associations between the PK and FK...
Read more >
Creating associations - AWS Systems Manager
Create a State Manager association by using the Systems Manager console, the AWS CLI, or Tools for Windows PowerShell.
Read more >
[Bug?] Express Entries can only see Associations in one ...
When a human Registers for an Event, they are in-effect creating one of these Experss Entries. One of the “questions” the human is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found