2013-08-25 181 views
1

我正在致力于calorie counter,我可以创建新的配方,但我在尝试更新配料时遇到问题。如何使用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 
    } 
    } 
} 

但是,如果我改变了成分的措施,从说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 
    } 
} 
+0

您使用哪种语言? parseFloat()和$ quantity不是MQL表达式。我猜你试图操纵CVT而不理解它们如何连接。 –

+0

我在javascript中构建查询,并将这些条目解引用值。它们不会传递到写入端点。 – Will

回答

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中,您应该让用户从配方中分别选择它们。 - 你需要/普通/主题添加到菜,你创建(但不与CVT变速器的成分)

这是伟大的,你正在使用沙箱,这样你不搞乱了生产&食谱数据库,而你调试。

+0

这适用于更新数量,但是相同的表单似乎不适用于更新单位。你能告诉我如何更新单位? – Will

+0

单位是一个实体,而不是一个值,因此您使用其ID或MID链接到它。我更新了查询。 –