How do I rename relationships in Neo4j?
Asked Answered
A

4

21

I realized only after importing a ton of nodes that I had created relationships called START, which is a reserved keyword. Querying the DB through the Cypher console hence always complains about the reserved keywords:

SyntaxException: reserved keyword "start n=node(0) match n<-[:START]-r return count(r)"

The only workaround that comes to mind is creating new copy relationships with a different name and then deleting the old ones.

Is there an easy way to rename all of these relationships or some way to escape reserved keywords in Cypher?

Aker answered 11/12, 2012 at 8:55 Comment(0)
A
7

You are right. You cannot rename an already existing relationship. You'd have to run through all relationships, create the new one in parallel (including all properties) and then remove the old one.

You may also want to consider quoting the reserved word START in your cypher queries with backticks and leave the relationships as they are:

start n=node(0) match n<-[:`START`]-r return count(r)
Apotheosize answered 11/12, 2012 at 10:21 Comment(1)
I want to keep same relationship ID to the newly added relationship. How could I?Herc
W
34

To do the equivalent of a rename, you can create a new one and delete the old one like so:

match (n1)-[old:`Start`]->(n2)
create (n1)-[new:StartDate]->(n2)
delete old

n.b. use backticks like those around `Start` to escape reserved keywords

Wanderjahr answered 20/2, 2014 at 2:12 Comment(2)
Best answer for Neo4j 2.x, note also that you can replace the "create" with "merge" if you want to avoid having duplicates if executing again.Rucksack
this approach won't keep the properties of previous relationOveractive
A
7

You are right. You cannot rename an already existing relationship. You'd have to run through all relationships, create the new one in parallel (including all properties) and then remove the old one.

You may also want to consider quoting the reserved word START in your cypher queries with backticks and leave the relationships as they are:

start n=node(0) match n<-[:`START`]-r return count(r)
Apotheosize answered 11/12, 2012 at 10:21 Comment(1)
I want to keep same relationship ID to the newly added relationship. How could I?Herc
S
6

You can use apoc plugin to rename labels and relationships. You can also use this to select a subset of relationships to rename. For eg below query will rename only relationship between Jim and Alistair.

MATCH (:Engineer {name: "Jim"})-[rel]->(:Engineer {name: "Alistair"})
WITH collect(rel) AS rels
CALL apoc.refactor.rename.type("COLLEAGUES", "FROLLEAGUES", rels)
YIELD committedOperations
RETURN committedOperations
Soilasoilage answered 26/9, 2021 at 3:36 Comment(0)
A
2
match (n1)-[old:`Start`]->(n2)
create (n1)-[new:StartDate {propName:old.propName, ...}]->(n2)
delete old
Aulos answered 31/1, 2019 at 18:45 Comment(1)
Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. From ReviewIntricate

© 2022 - 2024 — McMap. All rights reserved.