Armen Arzumanyan
Armen Arzumanyan

Reputation: 2053

neo4j cypher, strange errors when try to update and delete with ID(n)

I am using current version of neo4j, I am trying delete or update Node by graph id , when I try to delete passing as parameter for example name, it works normal, but I need delete with id one node with it is relation, and also update node.

String remove = "MATCH (n:Player {ID(n): {id} })-[r]-() DELETE n, r";
            ExecutionResult result = engine.execute(remove,
                   params); 

  and

 String query = "MERGE (n:Person {ID(n): {id}}) ON CREATE SET n.name={name}, n.surname={surname}, n.position={position} ON MATCH SET";

            ExecutionResult result = engine.execute(query,
                    params);

Always I am getting errors like

Invalid input '(': expected an identifier character, whitespace, ':' or '}' (line 1, column 20) "MERGE (n:Person {ID(n): {id}}) ON CREATE SET n.name={name}, n.surname={surname}, n.position={position} ON MATCH SET"

or

Invalid input '(': expected an identifier character, whitespace, ':' or '}' (line 1, column 20) "MATCH (n:Player {ID(n): {id} })-[r]-() DELETE n, r"

Usually I am used Spring data neo4j and did not find this type of errors.

Anyone have answers?

Thanks

Upvotes: 0

Views: 2106

Answers (2)

Jim Biard
Jim Biard

Reputation: 2272

Armen,

If you already know the Neo4j id value (the node number), then you can write your first query as

START n = node({id}) WITH n MATCH (n)-[r]-() DELETE n, r

However, the more I look at your code, the more I am wondering about your use of id. Do your nodes have a user-defined parameter named id? If so, then this all needs to be approached differently.

Grace and peace,

Jim

Upvotes: 1

jjaderberg
jjaderberg

Reputation: 9952

ID is a function that returns the internal id of a node or relationship, but in your query you use it as if it were a property on a node. The way to match a node by its internal id is

MATCH (n)
WHERE ID (n) = {id}

In your delete case you could do

MATCH (n:Player)-[r]-()
WHERE ID (n) = {id}
DELETE r, n

You can't assign the internal id to a node, Neo4j handles that internally, so it doesn't really make sense to use the ID function with MERGE. You can choose a different uniquely identifying property that you control and for which you create a unique constraint, then use that in most (probably all) places where you now use the internal id.

Upvotes: 1

Related Questions