cancel
Showing results for 
Search instead for 
Did you mean: 

Duplicate all relationships from one node to another

apwallace
Node

Apologies if this is a simple/newbie question - I've looked everywhere and cannot find an answer.

I would like to duplicate all the relationships that one node has, to another.
So for example, using the classic movie graph:

(mail:Movie{name:"You've Got Mail"})<-[:ActedIn{year:1998})]-(tom:Actor{name:"Tom Hanks"})<-[:Directed]-(nora:Director{name:"Nora Ephron"})

I'd like to be able to duplicate those relationships so that (meg:Actor{name:"Meg Ryan"}) is also connected to nora and mail with exact copies of those relationships - types and properties included.

Considerations:

  1. the relationships that Tom has are both incoming and outgoing
  2. Meg a is pre-existing Node - I'm not trying to create that ite in this query
  3. My real graph has many different relationship types, so for all intents and purposes, the code needs to be able to treat the relationship type as arbitrary input

I figure there is some combination of FOREACH and apoc's create.relationship() procedure but i cannot figure it out!

Any help gratefully received. this can't be something that's never come up before!

Thanks

1 REPLY 1

Yes you're right.
Easiest via 2 runs one per direction

MATCH (c:Actor {name:"Meg Ryan"})
MATCH (a:Movie)-[rel]->(b:Actor {name:"Tom Hanks"})
call apoc.create.relationship(a,type(rel),properties(rel), c) yield relationship 
return count(*)

For other nodes like the director, just extend the pattern and add a second create call.
But watch out for cardinalities so that you don't accidentally create multiple rels of the same type.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.