I have a method called create_node
that will create a new node if it doesn't already exist in my database. That method works when I test it with hard-coded values in the code snippet below:
next_node = session.read_transaction(get_node_by_name, 'Greve')
if next_node is None:
wikidata_results = find_in_wikidata('Q2044', 'Greve') # check if this is a valid entity in wikidata
if wikidata_results:
next_uri = wikidata_results[0]['location']['value']
node_result = session.write_transaction(create_node, next_uri)
if node_result:
print("Created node in db for {n}".format(
n=node_result['name']))
I know that method works, because I'm able to find the node that was created by querying my db in the Desktop app using cypher like this:
MATCH(n:Resource {name: 'Greve}) return n
But when I plug this into this part of my code, it is not saving the nodes:
cities = session.run("MATCH (a:`Location:City`) RETURN a.name AS name")
city_names = [record["name"] for record in cities]
for city in city_names:
current_node = session.read_transaction(get_node_by_name, city)
current_uri = os.path.basename(current_node[0]["uri"])
go_next_list = get_go_next_list(city)
if go_next_list:
for go_next_location in go_next_list:
go_next_location = clean_place(go_next_location)
# check if current link already has a node in the graph
next_node = session.read_transaction(get_node_by_name, go_next_location)
if next_node is not None:
result = session.write_transaction(create_relationship, city, go_next_location, "GO_NEXT")
for record in result:
print("Created {r} relationship between: {n1} and {n2}".format(
r=record['r'], n1=record['n1'], n2=record['n2']))
else:
wikidata_results = find_in_wikidata(current_uri, go_next_location)
if wikidata_results:
next_uri = wikidata_results[0]['location']['value']
node_result = session.write_transaction(create_node, next_uri)
if node_result:
print("Created node in db for {n}".format(
n=node_result['name']))
city_names.append(go_next_location)
session.write_transaction(create_relationship, city, go_next_location, "GO_NEXT")
else:
print("No wikidata results for item %s" % (go_next_location))
It seems to be working, because it's printing the statement I added after the transaction where I create the node (i.e. "Created node in db for {n}"). But any nodes that get created here are not being saved to the database. If I try to reference them later, I get an error and when I try to find them in my desktop app (like before: MATCH(n:Resource {name: 'Greve}) return n
), they aren't there.
I don't understand why this would work in the first case, but not in the larger context. I've also tried this with the py2neo library, and I run into the same issue where nodes I create don't get saved to the database. Is there anything I can do to debug this?