【发布时间】:2020-03-31 21:58:30
【问题描述】:
我想建立一个表示:An item is composed by a specific amount of others items 的续集关系。
数据库表
Item (itemId, name)
Ingredient (ingredientId, itemParentId, itemChildrenId, amount)
序列化模型
// Item.js
class Item extends Sequelize.Model { }
Item.init({
itemId: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: Sequelize.STRING,
}, {
sequelize: db,
})
// Ingredient.js
class Ingredient extends Sequelize.Model { }
Ingredient.init({
ingredientId: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
amount: Sequelize.INTEGER,
}, {
sequelize: db,
})
我只是想编写正确的 sequelize 关联以匹配我的数据库逻辑,所以我尝试了:
// Association.js
Item.belongsToMany(Item, { through: Ingredient, as: 'ingredients', foreignKey: 'itemParentId' })
但是我遇到了这个错误Unknown column 'ingredients->ingredient.ingredientItemId' in 'field list',这是真的,但我不知道如何指定正确的键/列。
请帮忙!
【问题讨论】:
标签: javascript node.js sequelize.js