Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating node attribute after object traversal is causing data loss #788

Open
slinkish opened this issue Feb 3, 2020 · 1 comment
Open

Comments

@slinkish
Copy link

slinkish commented Feb 3, 2020

I have nodes with a few attributes for example:

(_35682:LinkId {available: true, interface: '0/0/44', ip: '1.1.1.1', visible: true, x: 5, y: 5})

I am updating the availability in the following manner:
(node_obj is py2neo Node object that is directly linked to Node 35682)

local_links = graph.relationships.match((node_obj,))
int_nodes = [link.nodes[1]                                                  
                     for link in local_links]
for interface in int_nodes:                                                                                         
    interface['available'] = False                                      
    graph.push(interface)

The result from this code is:
(_35682:LinkId {available: false})

All other data or attributes are wiped in the neo4j database for this node.

Anytime I run through the process by hand and view the results and objects the code works as expected. Only faults when all lines run together.

I have worked around this issue with the following update :

local_links = graph.relationships.match((node_obj,))
int_nodes = [link.nodes[1]                                                  
                     for link in local_links]
for interface in int_nodes:
    graph.pull(interface)                                                                                         
    interface['available'] = False                                      
    graph.push(interface)

Not sure if this is expected behavior, I'm fairly new to this library but seems odd that data is loss with no exceptions or warnings. Any guidance is appreciated.

Hit issue with version: py2neo (4.3.0)

@ljluestc
Copy link

To avoid this issue, you should ensure that all attributes of the node are loaded before updating it. You can do this by explicitly pulling the node data from the database before making any changes. Your workaround using graph.pull(interface) is correct, but let's refine it a bit:

from py2neo import Graph

# Assuming `graph` is your Graph object and `node_obj` is the node you're starting from
local_links = graph.relationships.match((node_obj,))
int_nodes = [link.nodes[1] for link in local_links]

for interface in int_nodes:
    # Pull the latest data from the database to ensure all attributes are loaded
    graph.pull(interface)
    
    # Update the 'available' attribute
    interface['available'] = False
    
    # Push the updated node back to the database
    graph.push(interface)
Explanation of the Solution:
graph.pull(interface): This ensures that the interface object has all the latest attributes from the database.

interface['available'] = False: This updates the available attribute.

graph.push(interface): This pushes the updated node back to the database, ensuring that all attributes are preserved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants