Repository navigation
Description
What you are doing?
const Sequelize = require('sequelize')
const sequelize = new Sequelize('blogapplication', null, null, {
dialect: 'postgres',
})
var Product = sequelize.define('product', {
title: Sequelize.STRING
});
var User = sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
});
Product.belongsTo(User);
// Also works for `hasOne`
sequelize.sync({force:true})
.then(x => {
return Product.create({
title: 'Chair',
User: {
first_name: 'Mick',
last_name: 'Broadstone'
}
}, {
include: [ User ]
});
})What do you expect to happen?
I tried to do creating with associations as is specified in the Sequelize documentation, this is the example code from the doc, the only difference is that I don't use this.sequelize.define, but use sequelize.define instead (I got a error with this and wasn't sure what to bind it to).
What is actually happening?
No creation of the associations (User) is done.
SQL output:
Executing (default): INSERT INTO "products" ("id","title","createdAt","updatedAt") VALUES (DEFAULT,'Chair','2016-11-18 06:43:43.662 +00:00','2016-11-18 06:43:43.662 +00:00') RETURNING *;
How it does work
This code does work when I write user exactly as it was named in sequelize.define, see working code below. Not sure if this is behavior is because I didn't use this.sequelize or a mistake in the documentation.
const Sequelize = require('sequelize')
const sequelize = new Sequelize('blogapplication', null, null, {
dialect: 'postgres',
})
var Product = sequelize.define('product', {
title: Sequelize.STRING
});
var User = sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
});
Product.belongsTo(User);
// Also works for `hasOne`
sequelize.sync({force:true})
.then(x => {
return Product.create({
title: 'Chair',
user: {
first_name: 'Mick',
last_name: 'Broadstone'
}
}, {
include: [ User ]
});
})SQL output:
Executing (default): INSERT INTO "users" ("id","first_name","last_name","createdAt","updatedAt") VALUES (DEFAULT,'Mick','Broadstone','2016-11-18 06:52:11.822 +00:00','2016-11-18 06:52:11.822 +00:00') RETURNING *;
Executing (default): INSERT INTO "products" ("id","title","createdAt","updatedAt","userId") VALUES (DEFAULT,'Chair','2016-11-18 06:52:11.818 +00:00','2016-11-18 06:52:11.818 +00:00',1) RETURNING *;
__Dialect: postgres
__Database version: 6.1.0 (pg node module)
__Sequelize version: 3.24.8
Activity
janmeier commentedon Nov 18, 2016
The name of the attribute (
User) has to match the name of the association (user)So it's a bug in the documentation :) - PR welcome
[ci skip] Update associations.md (#6885)
mistertjen commentedon Nov 18, 2016
Cool, made the PR, thanks for the quick reply and merge!