[bug?] Creating with associations
See original GitHub issueI 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:
- Created 8 years ago
- Reactions:3
- Comments:20 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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