0

I'm using py2neo (using the Relationship.push() method) to update the properties of existing relationships. The relationship properties are updated successfully, but the related nodes get emptied of all their properties. Is this standard behavior?

Thanks.

Ken Adey
  • 11
  • 1

1 Answers1

0

I'm not able to recreate what you describe using the code below:

>>> from py2neo import *
>>> graph = Graph(password="p4ssw0rd")
>>> a = Node(name="Alice")
>>> b = Node(name="Bob")
>>> ab = Relationship(a, "KNOWS", b)
>>> graph.create(ab)
>>> remote(a)._id
1
>>> graph.evaluate("MATCH (n) WHERE id(n) = {x} RETURN n", x=1)
(alice {name:"Alice"})
>>> ab["since"] = 1999
>>> graph.push(ab)
>>> graph.evaluate("MATCH (n) WHERE id(n) = {x} RETURN n", x=1)
(alice {name:"Alice"})

That said, bear in mind that what you describe as pushing a relationship is in fact pushing the entire subgraph consisting of the relationship plus its start and end nodes. Therefore if the local copies of those nodes contain no properties, this will be taken as a signal to update the remote nodes as well by removing the remote properties.

Because of this "entire subgraph update" behaviour, you'll need to make sure that the local copies of your nodes are up-to-date before pushing them. Perhaps by pulling them first. There is no higher-level API operation to just push the relationship and ignore the nodes, these operations work on whole subgraphs. To do otherwise, you'll need to drop into Cypher.

Nigel Small
  • 4,475
  • 1
  • 17
  • 15
  • Thanks Nigel, that makes sense, as I do not have local copies of the related nodes. It's too bad that I can't just update the relationship, because what I'm doing is implementing a regular process to remove old data based on time. If I had to pull the nodes of every relationship I need to update, that could get expensive. So Cypher it is. Thanks again. – Ken Adey Jul 08 '16 at 12:14
  • Note that the Cypher for this should be quite straightforward. Something like `graph.run("MATCH ()-[r]->() WHERE id(r) = {x} SET r = {p}", x=123, p={"foo": "bar"})`. Alternatively, you could use the `+=` operator if you want to merge in a new set of properties instead of replacing them: `graph.run("MATCH ()-[r]->() WHERE id(r) = {x} SET r += {p}", x=123, p={"foo": "bar"})`. – Nigel Small Jul 13 '16 at 09:51