1

我正在研究卡路里计数器,我可以创建新食谱,但在尝试更新成分时遇到问题。

我可以使用以下 mql 创建成分:

{
  id: recipeId,
  '/food/recipe/ingredients': {
    create: 'unless_exists',
    id: null,
    quantity: parseFloat( row.$quantity.val() ),
    unit: {
      id: row.$units.val()
    },
    ingredient: {
      id: row.ingredientId
    }
  }
}

但是,如果我更改成分的量度,例如从 2 杯到 3 杯,则此查询将创建一种新成分。我一直在尝试更新条目。到目前为止,我已经尝试过:

{
  connect: 'update',
  id: row.rowId,
  type: '/food/recipe_ingredient',
  quantity: parseFloat( row.$quantity.val() ),
  unit: {
    id: row.$units.val()
  },
  ingredient: {
    id: row.ingredientId
  }
}

这会导致错误:Can't use [], [{}] or {} in a write

{
  connect: 'update',
  id: row.rowId,
  type: '/food/recipe_ingredient',
  quantity: parseFloat( row.$quantity.val() )
}

导致错误:Can't use 'connect' at the root of the query.

 {
   id: recipeId,
   '/food/recipe/ingredients': {
     connect: 'update',
     id: row.rowId,
     type: '/food/recipe_ingredient',
     quantity: parseFloat( row.$quantity.val() )
   }
 }

我得到错误:Can't use 'connect': 'update' on a non-unique master property

{
  id: row.rowId,
  type: '/food/recipe_ingredient',
  quantity: {
    connect: 'update',
    value: parseFloat( row.$quantity.val() )                                                                                               
  }
}

导致错误:This value is already in use. Please delete it first.删除旧成分并添加新成分的唯一方法是什么?

如果唯一的方法是删除旧条目并替换它们,有没有办法一次删除所有成分,而不是单独使用如下查询:

{
  id: recipeId,
  '/food/recipe/ingredients': {
    connect: 'delete',
    id: row.rowId
  }
}
4

1 回答 1

1

它不是一个“子对象”,而是一个独立的节点。我建议阅读有关CVT或中介类型的信息。要更改其属性,您需要将连接移动到内部查询。

这是我的披萨面团配方中的酵母成分节点

此查询将更新酵母的数量(假设只有一个成分节点指定酵母):

[{
  "id": "/m/0wh8nkh",
  "/food/recipe/ingredients": [{
    "ingredient": {
      "id": "/en/yeast"
    },
    "quantity": {
      "connect": "update",
      "value": 2
    },
    "unit": {
      "id": "/en/teaspoon",
      "connect": "update"
    }
  }]
}]

但我建议不要指望成分是唯一的,而是获取并保存成分节点的 ID,以便您可以在查询中明确引用它们。

其他一些注意事项: - 你不应该为每个食谱创建新的菜肴。许多菜肴应该已经存在于 Freebase 中,您应该让用户从食谱中单独选择它们。- 您需要将 /common/topic 添加到您创建的菜肴和食谱中(而不是添加到成分的 CVT)

使用沙箱非常好,这样您在调试时就不会弄乱生产数据库。

于 2013-08-26T15:59:02.713 回答