1

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

sten
  • 7,028
  • 9
  • 41
  • 63

1 Answers1

2

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
    
sentientcabbage
  • 466
  • 2
  • 6