Creating several of the same relationship types from a node in python using neo4j-driver

46 Views Asked by At

I'm running into this issue while trying to mass load data from a cypher script using the neo4j-driver for python. Lets say I have a node X, I should be able to do the following:

(X)-[:IS_FRIENDS]->(Y)

(X)-[:IS_FRIENDS]->(Z)

i.e. have multiple of the same relationship type coming from a node. But when I try to run a script (~5k Inserts, line-by-line), it runs, but it doesn't allow more than one of the same relationship type per node. This isn't an issue if I'm running the cypher query directly into the graph. I also found this (https://www.lyonwj.com/LazyWebCypher/) which doesn't have this problem, but I need to have the script run through python. The graph ends up missing ~1k Relationships.

The code in question:

from neo4j import GraphDatabase, basic_auth

neo_driver = GraphDatabase.driver(url, auth=basic_auth(us, pw))
neo_db = neo_driver.session()

with open(cypher_file, 'r') as f:
       for line in f:
            x = neo_db.run(line)
1

There are 1 best solutions below

0
On

So this was a common problem with py2neo so I assumed I was facing the same problem using the neo4j driver, but it turns out my problem was something else. Either way I managed to fix this forcing queued statements through after every query, this isn't very optimal but it works as a temporary solution.

This is done by using the following function:

neo_db.sync()

More can be found here: https://neo4j.com/docs/api/python-driver/current/transactions.html#neo4j.neo4j.Transaction.Transaction.run