简体   繁体   English

如何使用MQL更新子对象

[英]How to update subobject with mql

I'm working on a calorie counter and I can create new recipes, but I am having problems trying to update an ingredient. 我正在研究卡路里计数器 ,可以创建新食谱,但是尝试更新成分时遇到问题。

I can create an ingredient with the following mql: 我可以使用以下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
    }
  }
}

However if I change an ingredient's measure, say from 2 to 3 cups, this query will create a new ingredient. 但是,如果我将成分的量度从2杯更改为3杯,则此查询将创建新的成分。 I've been trying to update the entry. 我一直在尝试更新条目。 So far I've tried: 到目前为止,我已经尝试过:

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

This results in the error: Can't use [], [{}] or {} in a write . 这将导致错误: Can't use [], [{}] or {} in a write

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

Results in the error: Can't use 'connect' at the root of the query . 导致错误: 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() )
   }
 }

I get the error: Can't use 'connect': 'update' on a non-unique master property . 我收到错误: 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() )                                                                                               
  }
}

Results in the error: This value is already in use. Please delete it first. 导致错误: This value is already in use. Please delete it first. This value is already in use. Please delete it first. Is the only way to delete the old ingredient and add a new one? 是删除旧成分并添加新成分的唯一方法吗?

If the only way is to delete the old entries and replace them, is there any way to remove all the ingredients at once rather than individually with queries like: 如果唯一的方法是删除旧条目并替换它们,那么有什么方法可以一次删除所有成分,而不是像这样的查询单独删除:

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

It's not a "subobject" but a separate node in its own right. 它不是一个“子对象”,而是一个单独的节点。 I'd suggest reading about CVTs or mediator types. 我建议阅读有关CVT或调解器类型的文章。 To change its properties you need to move the connect to the inner query. 要更改其属性,您需要将连接移至内部查询。

Here's the yeast ingredient node from my pizza dough recipe 这是我的披萨面团配方中的酵母成分节点

This query will update the quantity for the yeast (assuming there's only one ingredient node specifying yeast): 此查询将更新酵母的数量(假设只有一个成分节点指定酵母):

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

but rather than counting on ingredients being unique, I'd suggest fetching and saving the IDs of the ingredient nodes so that you can refer to them explicitly in your query. 但是建议不要获取成分的唯一性,而是建议获取并保存成分节点的ID,以便您可以在查询中显式引用它们。

Some other notes: - you shouldn't be creating new Dishes for every Recipe. 其他一些注意事项:-您不应该为每个食谱创建新的菜式。 Many dishes should already exist in Freebase and you should let the user select them separately from the recipe. Freebase中应该已经存在许多菜肴,您应该让用户从食谱中单独选择它们。 - You need to add /common/topic to Dishes & Recipes that you create (but not to the CVTs for the ingredients) -您需要在创建的菜式和食谱中添加/ common / topic(但不必在CVT中添加成分)

It's great that you're using the sandbox so that you're not messing up the production database while you debug. 很好,您正在使用沙箱,以便在调试时不会搞乱生产数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM