【发布时间】:2018-10-12 17:33:00
【问题描述】:
我的模型:
Recipe (id, name)
Ingredient (id, name)
Recipe_Ingredient (recipeId, ingredientId, quantity)
我的联想:
Recipe.belongsToMany(Ingredient, { through: Recipe_Ingredient })
Ingredient.belongsToMany(Recipe, { through: Recipe_Ingredient })
我的问题:
我如何创建一个包含一些成分及其数量的食谱?
我试过了:
Recipe.create({
name: 'Pizza',
ingredients:[
{
name: 'mozarella',
recipe_ingredients: {
quantity: 5
}
}
]
}, {
include:[Ingredient]
})
为配方、成分和配方_成分创建记录。唯一的问题是数量的值不是从数据源收集的。
【问题讨论】:
-
如果在创建中指定
through会怎样?include:[ { model: Ingredient, through: Recipe_Ingredient } ]. -
可能还想尝试
receipe_ingredient(不是复数),因为它不是数组... -
@doublesharp 不起作用,表名也被冻结,所以复数没有问题
-
我相信你需要更新你的
include:{ include: [{ association: Ingredient, include: [ Recipe_Ingredient ] }] } -
@mcranston18 我试过了,但它并没有真正起作用。你有一个可行的例子吗?