Reputation: 2327
I am working with sequelize.js
, Where I have a table (say User
) with different string fields and a many-to-many relation with another table (say Nationality
).
I am trying to insert a new row of User
with all the sub-element and the nationality, in one single instruction. Now I am doing like this:
models.Nation.create({codice:"IT",name:"Italia"}).then(function(italy){
models.User.create({username:"local_u",nation:italy}).then(function(){
});
});
If I look at the database, at the newly created User
row: the field
NationId` is not set.
I know I can use the function setNation
of sequelize, but I wonder if I can do it within one single User.create
.
Upvotes: 2
Views: 1417
Reputation: 871
Yes you can create the user entity with nation in one step using include. Something like this (code not tested):
models.Nation.create({ codice: "IT", name: "Italia" }).then(function (italy) {
models.User.create({
username: "local_u",
nation: italy
}, {
include: [models.nation]
}
).then(function () {/*Do Something*/});
});
It's an old question but hopefully will help someone.
Upvotes: 1