cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Copy all relationships r that relate a to c in (a {uuid1})-[r]-(c), for use in (b {uuid2})-[r]-(c)

snellad1
Node Link

Hi,

I am trying to copy all r{} where (a {uuid1})-[r]-(c) for use in (b {uuid2})-[r]-(c), while maintaining the original (a)-[r]-(c). It's for versioning purposes - b is an updated node of a, and I want to copy over all relationships a had over to b.
I have uuid uniqueness constraints in place for both nodes and relationships.

I have read and tested how to move relationships from a to b with apoc.refactor.to and apoc.refactor.from. I've also tried apoc.refactor.cloneNodesWithRelationships.
But I've yet seen how to clone relationships without creating unwanted nodes.

Any suggestions?
Many thanks.

PS, Before posting, I've read the following:

https://neo4j-contrib.github.io/neo4j-apoc-procedures/

6 REPLIES 6

MuddyBootsCode
Graph Steward

Did you ever get any good feedback on this? Or figure it out? I've got a similar situation.

Michael can you add more details? Is this a case where you only need to copy relationships (but direct them to another node) or do you need to copy nodes as well?

Andrew, it would be a situation where a node was copied but given a different name and then had an exact copy of all the relationships the other node did.

MuddyBootsCode
Graph Steward

I'm giving this a shot
call apoc.refactor.cloneNodesWithRelationships([node1,node2,…​]).
This is my current query:

create (n:Lease {id: "Test2"})
with n as newLease
match (m:Lease {id: "Test"})
CALL apoc.refactor.cloneNodesWithRelationships([newLease,m]) YIELD input, output
RETURN *

So far it returns a copy of the node but none of the relationships. it makes sense as some nodes have a unique ID constraint. This is basically needs to be a copy and past with a rename. I hope this helps.

MuddyBootsCode
Graph Steward

Ok this is now working. The only issue is that if you have a uniqueness constraint on a node ID then if fails.

match (n:Lease {id: "Test"})
call apoc.refactor.cloneNodesWithRelationships([n]) yield output, input
set output.id = "Test2"
return *

You may be able to use just cloneNodes(), as this has some optional parameters that will let it clone relationships as well, and skip properties in the cloning (which should get around the unique id issue).

You can see the entire signature for usage with:

call apoc.help('cloneNodes')

Example of usage, skipping the id property (confirmed that this doesn't trigger unique constraint violations):

match (n:Lease {id: "Test"})
call apoc.refactor.cloneNodes([n], true, ['id']) yield output, input, error
set output.id = "Test2"
return input, output, error