cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

python neo4j driver exception while passing node label as a query param

I have written below cypher query to merge node dynamically using below query

from neo4j import GraphDatabase

# connect to the graph
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

# define parameters
parameters = {
"id": 123,
"label": "Person",
"attributes": {
"name": "John Doe",
"age": 30,
"gender": "male"
}
}

# query for nodes
query = """
MATCH (n: $label {id: $id})
WITH n, $attributes AS props
SET n += props
RETURN n
"""

with driver.session() as session:
result = session.run(query, parameters).data()
print(result)

But here I am getting exception, I am using neo4j 5.x and python driver version 5.2.0, can someone help me here

{code: Neo.ClientError.Statement.SyntaxError} {message: Invalid input '{': expected "%",

6 REPLIES 6

Hello, node labels cannot be passed as parameters.

any other way apart string formating to pass labels dynamically

It's not gonna perform great but something like:

MATCH (n {id: $id}) WHERE $label IN labels(n)
SET n += $attributes

@florent_biville   Hey I was looking for Merge query not the Match one and it seems you just copied from stackoverflow.

I did not copy anything from StackOverflow, what are you suggesting here?

I copied your initial query, removed the unneeded WITH and made it so you could use the label as a parameter.

Replace MATCH with MERGE and it should work (as long as the ID values are unique within the whole database). If IDs are only unique per label, then you'll need another approach, as detailed by my colleague Rouven below.

There have been many questions around this in the past. I compiled some of the stack overflow questions I found revolving this. The last one might be interesting as it shows a way around this limitation. But please beware of the caveats.