Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-30-2023 01:32 AM
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 "%",
01-30-2023 01:33 AM
Hello, node labels cannot be passed as parameters.
01-30-2023 01:38 AM
any other way apart string formating to pass labels dynamically
01-30-2023 01:40 AM - edited 01-30-2023 01:42 AM
It's not gonna perform great but something like:
MATCH (n {id: $id}) WHERE $label IN labels(n)
SET n += $attributes
01-30-2023 02:44 AM
@florent_biville Hey I was looking for Merge query not the Match one and it seems you just copied from stackoverflow.
01-30-2023 03:23 AM - edited 01-30-2023 03:28 AM
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.
01-30-2023 01:43 AM
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.
All the sessions of the conference are now available online