# Nodes are not saved using Python driver

**URL:** <https://community.neo4j.com/t/nodes-are-not-saved-using-python-driver/43183>\
**Category:** Python\
**Created:** [August 20, 2021, 5:12am UTC](https://community.neo4j.com/t/nodes-are-not-saved-using-python-driver/43183 "2021-08-20T05:12:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![geoffrey.hoffman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/geoffrey.hoffman/32/19588_2.png) [@geoffrey.hoffman](https://community.neo4j.com/u/geoffrey.hoffman)\
**Post date:** [August 20, 2021, 5:12am UTC](https://community.neo4j.com/t/nodes-are-not-saved-using-python-driver/43183/1 "2021-08-20T05:12:20Z")

</div>

My CREATE queries are not saving to the database.

I wrote some Python code using [the official driver](https://neo4j.com/developer/python/). 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.

```auto
    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?

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [August 20, 2021, 6:28am UTC](https://community.neo4j.com/t/nodes-are-not-saved-using-python-driver/43183/2 "2021-08-20T06:28:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [August 20, 2021, 6:35am UTC](https://community.neo4j.com/t/nodes-are-not-saved-using-python-driver/43183/3 "2021-08-20T06:35:27Z")

</div>

works for me

```auto
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

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

```

and cleaned up code

```auto
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

```

---

<div class="post-metadata">

**Author:** ![geoffrey.hoffman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/geoffrey.hoffman/32/19588_2.png) [@geoffrey.hoffman](https://community.neo4j.com/u/geoffrey.hoffman)\
**Post date:** [August 21, 2021, 6:19pm UTC](https://community.neo4j.com/t/nodes-are-not-saved-using-python-driver/43183/4 "2021-08-21T18:19:01Z")

</div>

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.
