quantity
これは、加算列を追加した RecipeIngredientPivot テーブル スキーマです。
-----------------------------------------------------
id | ingredientID | recipeID | quantity
-----------------------------------------------------
| 1 | A001 | R001 | 100 |
| 2 | A002 | R001 | 50 |
| 3 | C004 | R001 | 23 |
| 4 | A001 | R002 | 75 |
ModifiablePivot の代わりに Pivot を使用するだけで、兄弟としてandquantity
を持つピボット テーブルに列を作成できました。ingredient_id
recipe_id
extension RecipeIngredientPivot: Pivot {
init(_ recipe: Recipe, _ ingredient: Ingredient, _ quantity: Double) throws {
self.recipeID = try recipe.requireID()
self.ingredientID = try ingredient.requireID()
self.quantity = quantity
}
}
ただし、レシピの数量を照会して取得する方法がわかりません。
extension Recipe {
var ingredients: Siblings<Recipe, Ingredient, RecipeIngredientPivot> {
return siblings()
}
var quantities: [Double] {
// Not sure how to get this.
}
}
どんな助けでも大歓迎です。