Sequelize works with reciprocal relationships.
So if you want to have Items belong to User
you probably want to have User has one Item
as well.
In your case you have associated User to Item, but not Item to User. So let's do it by adding :
Item.belongsTo(User);
User.hasOne(Item); // this line
Now Sequelize knows that every user has one Item, which can give us the opportunity to make JOIN
queries.
The way that the library handles the properties of the associations is by the table name. So in your case you will also need to rename Item
to item
, like this :
User.create({
name: 'Hobbyist',
age: 22,
item: {
// ^ item, not Items
name: 'Phone',
price: 199
}
}, {
include: [ Item ]
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…