Search code examples
neo4jpy2neo

Neo4j Py2neo not updating node properties


Writing python program to fetch existing Neo4j node and update properties using py2neov3 package.

Movie node has title & year properties. Have a python dictionary with list of movies to be added.

I have tried below options, movie node is getting added. But year property is not updated.

Option#1: Use Py2neo OGM. Start transaction, create Movie object, populate title, invoke merge, populate year, invoke push, finally commit

Option#2: Instead of OGM (Commented code below), use Node function, invoke merge and push.

I have done with above mentioned options, but it didn't work for me. Python version 3.5.2

Code:

try:  
tx = gdb.begin()   ##gdb is Graph object 
for x in moviedict.keys():
 m1 = Movie()
 m1.title = moviedict[x]['title']
 tx.merge(m1)
 m1.year = moviedict[x]['year']
 tx.graph.push(m1)       
tx.commit()
"""Option2 for x in moviedict.keys():
   m1 = Node('Movie',title=moviedict[x]['title'])
   gdb.merge(m1)
   m1['year'] = moviedict[x]['year']
   gdb.push(m1)
 """

Can anyone help me on this issue?
Your help is much appreciated.
Best Regards.


Solution

  • I didn't use dictionaries, because I wanted the example to be both short and runnable

        import py2neo
        import py2neo.ogm
    
        from py2neo import Graph
        from py2neo.ogm import GraphObject, Property
    
        class Movie(GraphObject):
            __primarykey__ = "title"
    
            title = Property()
            released = Property()
    
        def authenticateAndConnect():
            py2neo.authenticate('localhost:7474', 'user', 'password')
            return Graph('http://localhost:7474/default.graphdb/data/')     
    
        def foo():
            graph = authenticateAndConnect()
            movie = Movie.select(graph).where("_.title = 'The Matrix Reloaded'").first()
            movie.released = 2017
            graph.push(movie)
    
        if __name__ == '__main__':
            foo()