Search code examples
csvneo4jcypherpy2neo

How to pass parameter as property name in py2neo?


I get error while passing parameter as property name. I did not get any error while passing parameter as property value by writing {param}. But this is not working in the case of passing parameter as property name.

Here is my code.

query = 'Merge(c1:Customer{user_id: {user_id1},{user_id2}:{cell}})'
g.run(query, user_id1=int(row['user_id']), user_id2=str(cidx),cell=cell) 

Here cidx, cell, row['user_id'] are parameters.

{user_id1} parameter is working.

But it does not take {user_id2} as parameter to add property name


Solution

  • Cypher queries cannot be parametrized by Property names.

    Parameters can be used for:

    • literals and expressions
    • node and relationship ids
    • for explicit indexes only: index values and queries

    Parameters cannot be used for the following constructs, as these form part of the query structure that is compiled into a query plan:

    • property keys; so, MATCH (n) WHERE n.$param = 'something' is invalid

    • relationship types

    • labels

    Refer Neo4j Documentation for more details.

    EDIT:

    You can format string to add property name as:

    query = 'Merge(c1:Customer{user_id: {user_id1}, %s :{cell}})' % str(cidx)
    

    Remove the parameter user_id2 from run method:

    g.run(query, user_id1=int(row['user_id']), cell=cell)