agens-graph

How to avoid duplicate vertex node on AgensGraph?


I want to create edge on two vertexes.

agens=# create (:v1{id:1}), (:v1{id:2});
GRAPH WRITE (INSERT VERTEX 2, INSERT EDGE 0)
agens=# create (:v1{id:1})-[:e1{id:3}]->(:v1{id:2});
GRAPH WRITE (INSERT VERTEX 2, INSERT EDGE 1)
agens=# match (n:v1) return n;
        n         
------------------
 v1[3.1]{"id": 1}
 v1[3.2]{"id": 2}
 v1[3.3]{"id": 1}
 v1[3.4]{"id": 2}
(4 rows)

But, There is duplicated vertex on it.

How to avoid duplicate vertex node on AgensGraph?


Solution

  • First, find the vertexes using match

    After, add edge using found vertexes.

    agens=# create (:v1{id:1}), (:v1{id:2});
    GRAPH WRITE (INSERT VERTEX 2, INSERT EDGE 0)
    agens=# match (n1:v1{id:1}), (n2:v1{id:2}) create (n1)-[:e1{id:3}]->(n2);
    GRAPH WRITE (INSERT VERTEX 0, INSERT EDGE 1)
    agens=# match (n:v1) return n;
            n         
    ------------------
     v1[3.1]{"id": 1}
     v1[3.2]{"id": 2}
    (2 rows)