Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent creating duplicates of edges between the same vertices in OrientDB?

I have vertex "Person" and edge "Knows". Here is SQL example of how I created it.

CREATE CLASS Person EXTENDS V;
CREATE PROPERTY Person.name STRING;

CREATE CLASS Knows EXTENDS E;

INSERT INTO Person (name) VALUES("John")
INSERT INTO Person (name) VALUES("Ann")
INSERT INTO Person (name) VALUES("Harry")

When I create an edge between John knows -> Ann by

CREATE EDGE Knows FROM (SELECT FROM Person WHERE name = "John") 
TO (SELECT FROM PERSON WHERE name = "Ann") 

it creates it and every thing is ok.

But problem occurs when I accidentially create edge several times.

For the relationship "Knows" duplicates are redundant, but for some other ones like "Visited" (John [Visited ->] New York) duplication of edges are desired feature if edge "Visited" has property "date".

I tried to solve it by adding unique index to edge "Knows", but after that I am able to create edge between only one pair of vertices.

And checking every time edge for existence before creation doesn't seem to me a good idea as well.

How to solve this in correct way?

like image 455
Xylian Avatar asked Feb 03 '18 18:02

Xylian


1 Answers

The straight forward solution is to create an index on EdgeClass[out, in]. To do this, you also have to define the schema for the edge class:

CREATE CLASS Knows EXTENDS E
CREATE PROPERTY Knows.out LINK Person
CREATE PROPERTY Knows.`in` LINK Person
CREATE INDEX Knows.out_in ON Knows (out, in) UNIQUE
like image 114
Luigi Dell'Aquila Avatar answered Oct 20 '22 16:10

Luigi Dell'Aquila