gremlin 在满足多重性约束的同时创建边,如果违反则删除现有边然后创建其他只是创建它

gremlin create edge while satisfying multiplicity constraint,if violated drop existing edge then create else just create it

我为解决我的问题而苦苦挣扎了一段时间

我有一个图表,其中定义了多重性约束。创建新边时,要么创建新边,要么由于数据更改而违反多重性约束。

现在,当数据发生变化时,我需要 delete/drop 现有边缘并创建一个新边缘。这是我的问题。我无法一次删除并创建边缘。

我一直在尝试的是这个 我正在通过 nodejs 的 node-gremlin 模块将查询发送到 gremlin 服务器。 我试图创建的关系是 [merchant]-1--(sells)--*-> [product] 。在给定的场景中,只有 1 个商家可以销售产品。当其他商家开始销售产品时。我需要更新它以反映它们之间的新关系。可能是因为以前没有人出售它,所以只需要创建新的优势。最后返回创建的边。

29 Jun 13:41:04 - [Error: An edge with the given label already exists on the in-vertex and the label [sells] is in-unique (Error 597)]
29 Jun
 13:41:04 - { text:
'g.V().has(sIdKey,sIdVal).inE(edgeLabel).drop();graph.tx().commit();g.V().has(fIdKey,fIdVal).outE(edgeLabel).inV().has(sIdKey,sIdVal).tryNext().orElseGet{g.V().has(fIdKey,fIdVal).next().addEdge(edgeLabel,g.V().has(sIdKey,sIdVal).next());};',
     params:
       { fIdKey: 'merchant_id',
         fIdVal: 20230,
         sIdKey: 'product_id',
         sIdVal: 184504,
         edgeLabel: 'sells' } }

我试图实现的流程是这样的

查找现有边是否存在 -> 删除现有边 -> 提交删除边命令 -> 创建新边 -> 提交新边。

在上面的查询中,我没有为添加边编写提交语句,因为我正在批量提交添加边。

我不知道如何着手解决这个问题。任何帮助都会很棒。

您需要 iterate() 在提交事务之前进行删除操作。

g.V().has(sIdKey,sIdVal).inE(edgeLabel).drop().iterate(); graph.tx().commit();

这是一个常见的绊脚石,之前已经讨论过: