Search code examples
pythonneo4jpy2neo

What is wrong with my Neo4j query for Py2neo?


This is my query for Py2neo for Neo4j database

MATCH (u:User),(p:Prize),(ca:Category) CREATE (ch:Challenge {chid:'dassdshhhhasdasda',challenge_title:'Exm 2015', total_question_per_user:200,challenge_status:1,timestamp:'1471516538.4643',date:'2016-08-18'}), (p)-[:BELONG {rank:3}]->(ch),(ca)-[:BELONG {percentage_question:20}]->(ch) WHERE u.username = 'xyz@gmail.com' AND p.pid = 'e766d8cd-26d1-4848-ac97-15c233caa4d4' AND ca.catname = 'nature'

But when I run it manually in Neo4j database command line then it show error

Invalid input 'H': expected 'i/I' (line 1, column 287 (offset: 286))
"MATCH (u:User),(p:Prize),(ca:Category) CREATE (ch:Challenge {chid:'dassdshhhhasdasda',challenge_title:'Exm 2015', total_question_per_user:200,challenge_status:1,timestamp:'1471516538.4643',date:'2016-08-18'}), (p)-[:BELONG {rank:3}]->(ch),(ca)-[:BELONG {percentage_question:20}]->(ch) WHERE u.username = 'xyz@gmail.com' AND p.pid = 'e766d8cd-26d1-4848-ac97-15c233caa4d4' AND ca.catname = 'nature'"

I want to use WHERE clauses, without WHERE I run this query like this then its working

MATCH (u:User {username:'xyz@gmail.com'}),(p:Prize{pid:'e766d8cd-26d1-4848-ac97-15c233caa4d4'}),(ca:Category {catname:'nature'}) CREATE (ch:Challenge {chid:'dassdsdjgjasdasdasda',challenge_title:'Exm 2015', total_question_per_user:200,challenge_status:1,timestamp:'1471516538.4643',date:'2016-08-18'}), (p)-[:BELONG {rank:3}]->(ch),(ca)-[:BELONG {percentage_question:20}]->(ch)

Solution

  • You are using the WHERE clause in the wrong spot. The where clause needs to be used in conjunction with the MATCH statement and not the CREATE.

    Something like this...

    MATCH (u:User),(p:Prize),(ca:Category) 
    WHERE u.username = 'xyz@gmail.com' 
    AND p.pid = 'e766d8cd-26d1-4848-ac97-15c233caa4d4' 
    AND ca.catname = 'nature'
    CREATE (ch:Challenge {chid:'dassdshhhhasdasda', 
        challenge_title:'Exm 2015', 
        total_question_per_user:200, 
        challenge_status:1, 
        timestamp:'1471516538.4643', 
        date:'2016-08-18'})
       ,(p)-[:BELONG {rank:3}]->(ch)
       ,(ca)-[:BELONG {percentage_question:20}]->(ch)