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.

Nodes are not saved using Python driver

My CREATE queries are not saving to the database.

I wrote some Python code using the official driver. I can step through the code and log the cypher, and even see the node returned to result has an id and a node in the debugger, but when I go to verify the nodes with MATCH in Neo4j Desktop, they're not there.

I tried to log the generated cypher, and when I paste the cypher from the log to Neo4j Desktop, they work - the nodes get created.

I can't for the life of me understand what I am doing wrong - I'm using code pasted from the offical docs. I tried with and without .consume() and also tried the tx.write_transaction( ) method.

    def create_some_node_with_related_node(self, name):
        with self.driver.session() as session:
            cypher = "MATCH (sn:SomeNode) " \
                     "WHERE sn.name=\"{name}\" " \
                     "CREATE (rn:RelatedNode{{propertyId:{property_id}, code:sn.code}})-[r:derivedFrom]->(sn) " \
                     "RETURN sn,r,rn".format(name=name, property_id=property_id)
            neo.log(cypher, 101)
            neo.incr('counter')
            try:
                result = session.run(cypher)
                single = result.single()
                return single[0]
            except:
                error = sys.exc_info()[0]
                print('Error:', error)
                return False

result is a <neo4j.work.summary.ResultSummary object at 0x7fc36de94d90> and single is a <Node id=6257615 labels=frozenset({'RelatedNode'}) properties={'property_id': 8, 'code': 'X19235195'}> But when I do MATCH(rn:RelatedNode) RETURN rn in desktop after my script completes, there's nothing there.

Yet if I copy the generated cypher from my log, paste it into desktop and run it, it works fine, and the node & relationship are generated as expected.

What am I doing wrong?

3 REPLIES 3

Can you try to not use python formatting for the values but rather proper cypher parameters like $name and pass them along when you execute?

I think something goes wrong there, btw. you can use triple quotes for your query string.

also where does your property_id come from?

works for me

from neo4j import GraphDatabase,basic_auth

driver = GraphDatabase.driver("bolt://localhost",auth=basic_auth("neo4j","test"))

def create_some_node_with_related_node(driver, name, property_id):
    with driver.session() as session:
        cypher = "MATCH (sn:SomeNode) " \
                    "WHERE sn.name=\"{name}\" " \
                    "CREATE (rn:RelatedNode{{propertyId:{property_id}, code:sn.code}})-[r:derivedFrom]->(sn) " \
                    "RETURN sn,r,rn".format(name=name, property_id=property_id)
        try:
            result = session.run(cypher)
            single = result.single()
            return single[0]
        except:
            error = sys.exc_info()[0]
            print('Error:', error)
            return False


with driver.session() as s:
    res = s.run("MERGE (n:SomeNode {name:$name, code:'foo'}) RETURN id(n)",name="test").single()
    print(res)

create_some_node_with_related_node(driver,"test",42)

with driver.session() as s:
    for res in s.run("MATCH (r:RelatedNode) RETURN r"):
        print(res)

output

python3 test2.py 
<Record id(n)=0>
<Record r=<Node id=1 labels=frozenset({'RelatedNode'}) properties={'propertyId': 42, 'code': 'foo'}>>

and cleaned up code

def create_some_node_with_related_node(driver, name, property_id):
    with driver.session() as session:
        cypher = """MATCH (sn:SomeNode) 
                    WHERE sn.name=$name
                    CREATE (rn:RelatedNode {propertyId:$property_id, code:sn.code})-[r:derivedFrom]->(sn) " \
                    RETURN sn,r,rn"""
        try:
            result = session.run(cypher, name=name, property_id=property_id)
            single = result.single()
            return single[0]
        except:
            error = sys.exc_info()[0]
            print('Error:', error)
            return False

I discovered the reason for my issue - I was connecting to a DNS name pointing to a sticky load balancer, and my CREATE / MERGE queries were almost always getting routed to FOLLOWER nodes!

Once I replaced the connection string with the LEADER directly, everything worked fine.