3

Is it possible to get an example to upsert an edge in orientdb. IF it does not exist is there a way to check if the edge exist, if it does then just update the edge else create a new edge. I am using Orientdb 2.1.13 version. Thank you

howard roark
  • 628
  • 7
  • 27

2 Answers2

5

via SQL you can use the basic UPDATE command

update written_by SET out = #9:2, in = #16:43, prop="gianni" UPSERT WHERE out = #9:2 and in = #16:43

http://orientdb.com/docs/last/SQL-Update.html

Ivan Mainetti
  • 1,982
  • 7
  • 13
  • basically it creates a different versions of the same record. How do I stop that from being created? – howard roark Jun 28 '16 at 12:54
  • 1
    I don't understand why you say that the query of @imainetti creates another record. I have tried and it doesn't create no one duplicate record. – Alessandro Rota Jun 28 '16 at 13:14
  • I deleted my previous comment. It creates a different version of same record for concurrency purposes. – howard roark Jun 28 '16 at 13:29
  • This is pretty useful, as the manual doesn't say much here. However i ttried (v3.0.0) and executing the same syntax gives me following exception: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.orienttechnologies.orient.core.db.record.OIdentifiable Here's my batch: BEGIN; LET $from = SELECT FROM Content WHERE uri='www.webgraph.com'; LET $to = SELECT FROM Content WHERE uri='webgraph1.com'; UPDATE Navigation SET out = $from, in = $to, timestamp = sysdate() UPSERT WHERE out = $from and in = $to; COMMIT; RETURN $edge; any Ideas? – Manuel Jan 20 '19 at 18:39
1

When you use "update written_by SET out = #9:2, in = #16:43, prop="gianni" UPSERT WHERE out = #9:2 and in = #16:43", it doesn't work properly for edges: it will create edge if it doesn't exist, but it will not create in and out properties in vertex, so, for example, you will not able to query MATCH. It works this way cos “The UPDATE/UPSERT works at document level, so it doesn't create the connections from the vertices. Using it, you will have a broken graph”, as authors said.

But you can use “upsert” for edges since version 3.0.1 and it will work properly – but you need to do the following:

Create unique index on edge_class (out, in) and – it's strange – The order is important! To do this, you need to create in and out properties first, otherwise db can't create index and there will be an exception when you will try to run command “Create index”. Then, use command CREATE EDGE UPSERT FROM TO .

In this case edge will be created only if it is not exists, and it will create in and out properties for vertex classes.

Maklyura
  • 21
  • 2