Search code examples
node.jstitangremlingremlin-server

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


I am struggling for quite some time to solve my problem which is like

I have a graph in which there are defined multiplicity constraints . When new edge is being created either a new edge can be created or it can violate the multiplicity constraint due to data being changed.

Now when data has changed I need to delete/drop the existing edge and create a new one. This is my problem. I am not able to drop and create the edge in single go.

What i have been trying is this I am sending the query to gremlin server via node-gremlin module of nodejs. relation i am trying to create is [merchant]-1--(sells)--*-> [product] . In given scenario only 1 merchant can sell a product . when some other merchant starts selling the product. I need to update it to reflect the new relation between them. It may be the case that no one was selling it previously so only new edge has to be created. finally the created edge is returned.

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' } }

The flow ii am trying to achieve is this

Find if existing edge is present -> delete the existing edge -> commit delete edge command --> create new edge --> commit new edge.

In the above query i have not written commit statement for add edge because i am committing add edges in bulk.

I am not able to figure out the how to proceed to solve this problem . any help would be great.


Solution

  • You need to iterate() on the drop operation before committing the transaction.

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

    This is a common stumbling block and has been discussed previously: Gremlin drop() isn't working via java api