如何使用mql更新子对象

时间:2013-08-25 22:02:30

标签: freebase mql mqlwrite

我正在研究calorie counter并且我可以创建新食谱,但我在尝试更新原料方面遇到了问题。

我可以使用以下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
  }
}

1 个答案:

答案 0 :(得分:1)

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

这是我的披萨面团node

中的酵母成分recipe

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

[{
  "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添加到Dishes&您创建的食谱(但不包括成分的CVT)

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