sten
sten

Reputation: 7486

conditional creations of relations?

I have a query to move relations, but if there are existing target relations they are not replaced, so I end up with two similar connections.

    match (new:state) where new.val = {new}
    match (b:state)-[r1:q]->(p:state)-[r2:q]->(n:state)
    where b.val = {blast} and p.val = {past} and n.val = {now}
    merge (b)-[nr1:q]->(new)-[nr2:q]->(n)
    set nr1.val = r1.value, nr2.val = r2.val
    delete r1, r2

if (b)-[:q]->(new) exists, nr1 does not override it, but creates new relation, so I end up with two .

I want to create nr1 or nr2 if r1 or r2 are not yet present and if present sum the values, otherwise just copy them

Upvotes: 1

Views: 93

Answers (1)

sentientcabbage
sentientcabbage

Reputation: 466

You can specify your two relationships in separate MERGE clauses:

    match (new:state) where new.val = {new}
    match (b:state)-[r1:q]->(p:state)-[r2:q]->(n:state)
    where b.val = {blast} and p.val = {past} and n.val = {now}
    merge (b)-[nr1:q]->(new)
    merge (new)-[nr2:q]->(n)
    set nr1.val = r1.value, nr2.val = r2.val
    delete r1, r2
    

Upvotes: 2

Related Questions