ArangoDB- duplicate edges

812 views Asked by At

I am using pyArango to create edges between two documents using the function

Graph.createEdge()

The problem I am facing is that ArangoDB allows to create duplicate edges with the same _to, _from pair. How to avoid this situation in ArangoDB

2

There are 2 answers

0
tariqdaouda On BEST ANSWER

An easy solution is to check before insertion using fetchFirstExample() on the edges collection:

try :
  edge = myEdgesCollection.fetchFirstExample({"_to": doc2._id, "_from": doc1._id})[0]
except :
  edge = myGraph("myEdgesCollection", doc1, doc2, {})
0
Daniel Krom On

This is impossible via some constraint role,

You can avoid it at the insert level:

When creating an edge, use the next query:

UPSERT {_from : @from, _to : @to}
INSERT {_from : @from, _to : @to, label : "knows"} 
UPDATE {} IN @@edgeCollection

If there's a match it won't create it (but notice, it will update it so leave the update object empty )