while reading sequelize doc association part, explanation below really confuses me
hasOne and belongsTo insert the association key in different models f
Actually it depends on how are you creating your tables. Via sync
or via migrations
.
For example
const User = sequelize.define('user', {
username: Sequelize.STRING,
});
const Address = sequelize.define('add', {
address: Sequelize.STRING,
});
User.hasOne(Address);
sequelize.sync({ force: true })
.then(() => User.create({
username: 'test'
}))
.then(item => {
console.log(item.dataValues.itemPrice);
});
This will create a userId field in the database
Note that I am use sync
. However if you are using migrations, you would have to create this on your own.
PS: Its better to use migrations
instead of sync
.