Neo4j Cypher Match with relationship direction error

Neo4j Cypher Match with relationship direction error

  • @Tsartsaris

    Posted on 2014-01-14

    ‚Äč‚ÄčToday I was having a look at the Neo4j google group and there is a question there that draw my attention. You can read about the question at this link or read below.
    Please tell me about the following query whether it is correct to delete relation and create relation with updating fields:
    These are working fine seperately

       START n=node(9443), m=node(9440), p=node(1)
       MATCH q=(p)-[r:MODIFY_TIME]->(m)
       DELETE r
       CREATE (n)-[s:MODIFY_TIME]->(m)
       SET m.changedDate = "13/01/14", m.changedTime = "10:00"
       RETURN s;

    I recreated the issue in my database and made some tests to see what the heck is going on. To recreate the issue in a more abstract way lets create 2 nodes and say that those nodes are n,m. We do not need them to have any properties. Assuming that n(ID)=1 and  m(ID)=2 lets create a relationship between n and m.

    START n=node(1), m=node(2)
    CREATE (n)-[r:MODIFY_TIME]->(m)
    RETURN r

    If we want to use MATCH to return the relationship between those 2 nodes we should do something like this

    START n=node(1), m=node(2)
    MATCH (n)-[r:MODIFY_TIME]->(m)
    RETURN r

    But there are times when we write a lot of code and we make a lot of tests that we forget some tiny details on the code we write. The issue here is wether we had the relationship directioned to n or m. If we try to match the relationship and by mistake we use the opposite direction the query will return nothing.

    START n=node(1), m=node(2)
    MATCH (n)<-[r:MODIFY_TIME]-(m)
    RETURN r

    If we want to be more forgiving on our selfs then we should use the MATCH clause with no direction at all since MATCH will return the relationship either way. So better use something like this

    START n=node(1), m=node(2)
    MATCH (n) - [r:MODIFY_TIME]  - (m)
    RETURN r

    As you can see at the example query above we dont explicitly declare a direction in the MATCH clause and regardless the direction we used to create the relationship this will return it.
    I hope I gave a good answer to the guy asking this in the Neo4j google group. If you have any issues , ideas or you find something wrong please tweet at the Graph Blog hash tag.

Tag-cloud

webNeo4Jphpd3jsubuntuworkcypherinternetbootstrapdevelopmentflaskpython

Social Me!

Twitter Logo LinkedIn Logo Google+ Logo Tumblr Logo