20

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?

ulkas
  • 5,748
  • 5
  • 33
  • 47

4 Answers4

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

JobJob
  • 3,822
  • 3
  • 33
  • 34
  • 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. – Andrew Lank Feb 05 '15 at 13:23
  • 7
    this approach won't keep the properties of previous relation – walv Jun 10 '16 at 21:01
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)
James
  • 11,654
  • 6
  • 52
  • 81
4

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
dicemaster
  • 1,203
  • 10
  • 22
2
match (n1)-[old:`Start`]->(n2)
create (n1)-[new:StartDate {propName:old.propName, ...}]->(n2)
delete old
Murph
  • 21
  • 3
  • 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 Review](https://stackoverflow.com/review/low-quality-posts/22085663) – Nick Feb 01 '19 at 03:14