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

Xylian :

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?

Luigi Dell'Aquila :

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=445059&siteId=1