0

I'm trying to make a social network and its my first web experience. I'm using Neo4j database and py2neo module. Now I want to find a node from my database and change some of it's properties. I'm using the code below,and i can run it with no errors .but it doesn't change anything in my database and i have no idea why... please help me if you can.

from py2neo import Graph
graph=Graph()
def edit_name(Uname,name):
person=graph.merge_one("Person","username",Uname)
person.cast(fname=name)
fracz
  • 20,536
  • 18
  • 103
  • 149
ali73
  • 415
  • 1
  • 5
  • 17

2 Answers2

0

merge_one will either return a matching node, or, if no matching node exists, create and return a new one. So, in your case, a matching node probably already exists.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • yeah it exists. and when i return the node ,it returns correctly. but "person.cast(fname=name)" doesn't work correctly – ali73 Mar 20 '15 at 16:29
0

Cast is for casting general Python objects to py2neo objects. For example, if you wanted to cast a Python dictionary to a py2neo Node object, you'd do:

from py2neo import Graph, Node
graph = Graph()

d = {'name':'Nicole', 'age':24}
nicole = Node.cast('Person', d)

However, you still need to pass nicole to Graph.create to actually create the node in the database:

graph.create(nicole)

Then, if you later retrieve this node from the database with Graph.merge_one and want to update properties:

nicole = graph.merge_one('Person', 'name', 'Nicole')
nicole['hair'] = 'blonde'

Then you need to push those changes to the graph; cast is inappropriate for updating properties on something that is already a py2neo Node object:

nicole.push()

TL;DR:

from py2neo import Graph
graph = Graph()

def edit_username(old_name, new_name):
    person = graph.merge_one('Person', 'username', old_name)
    person['username'] = new_name
    person.push()
Nicole White
  • 7,720
  • 29
  • 31