Can I use apoc.create.relationships with a relationship list?

apoc
cypher

(Tommy Cabrelli) #1

Hello everyone !
I'm trying to move every relationships of a node to an another.
Here is my query for now :

MATCH(n:Resource {name: 'source-resource'})-[r]-() with collect(distinct r) as relations,n
MATCH(n2:Resource {name: 'destination-resource'}) with n,n2,relations
CALL apoc.create.relationship(n, type(relations), NULL, n2) YIELD rel
return rel

Of course that doesn't work since apoc.create.relationship awaits a relationship argument instead of a List

Thanks in advance for your answers :slight_smile:


(Michael Hunger) #2

You also forgot to pass your n with the WITH

There is a dedicated procedure to move relationships to a new target,

apoc.refactor.to

this should work

MATCH(n2:Resource {name: 'destination-resource'})
MATCH (n:Resource {name: 'source-resource'})-[r]-()
CALL apoc.refactor.to(r, n2) YIELD rel
RETURN rel

see: https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_graph_refactoring_2


(Tommy Cabrelli) #3

Thank you very much Michael !
As I wanted every relationships (from and to) I also used apoc.refactor.from and ended up with that :

MATCH(n2:Resource {name: 'destionation-resource'})
MATCH(n:Resource {name:'source-resource'})<-[r]-()
OPTIONAL MATCH(n)-[r2]->()
CALL apoc.refactor.to(r, n2) YIELD input, output
CALL apoc.refactor.from(r2, n2) YIELD input AS i, output AS o
RETURN *

And it worked perfectly !
Thank you Michael. I took a look at your medium blog, great content I will read some of them :slight_smile:
Have a good day !