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: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=6)
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: Neo4j APOC Procedures User Guide
1 Like
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: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=6)
Have a good day !