Append Matched Relationships to node with Existing Relationships?

Hello again!
I have a relatively simple problem I can't figure out. I'm trying to "merge" relationships over from one node to another node with relationships of its own; Ideally, if the below worked I'd be in the clear but I get "variable r2 already defined" ;(

match (n:user {other_id:"me"})-[r]-() with r, n
match (temp:user {btcID: n.ID}) where not exists(temp.other_id) match (temp)-[r2]-() 
Create / Merge (n)-[r2]-(z) //or set r = r + r2
detach delete temp
return n

I'm trying to append the matched relationships connected to the temp node, switch them over to the existing "me" node, and then delete the temp node. Is there a way to do this without apoc?

Thanks again, really appreciate all thehelp received here :D

Hello @racket8484,

The relationship you want to create in the MERGE clause must be new so you cannot use the r2 one, but if you want to copy the data in it, you can extract them with a WITH clause and after the MERGE, you can SET the relation:)

Regards,
Cobra

2 Likes
MATCH (n:user {other_id:"me"})-[r]-()
MATCH (temp:user {btcID: n.ID})
WHERE NOT exists(temp.other_id)
MATCH (temp)-[r2]-(tgt) 
MERGE (n)-[rNew]-(tgt)
  ON CREATE SET rNew=r2 # will copy all properties over
  ON MATCH SET rNew=r2 # what to do with old r props? keep, replace, sum?
DETACH DELETE temp
RETURN n
1 Like