The current version of py2neo
doesn't allow for 'relationship objects', which would allow you to put parameters onto a relationship in the standard OOP-y way.
The OGM is very useful for getting started on a project, and for quick insert / retrieval transactions, but for most more complex transactions, you will want to write cypher to achieve your objective.
However, frequently you may wish to organize these queries on a GraphObject
, and sometimes you can extend GraphObject
to add a method to provide some required functionality.
In your use case, you may wish to add some methods like the below to get or set your relationship properties. I've written a quick demo for retrieving the properties of the relationship, you could add a similar method for setting the relationship, too.
from py2neo.ogm import (GraphObject,
INCOMING,
OUTGOING,
Property,
RelatedFrom,
RelatedTo,)
# this function makes this kind of thing tonnes easier
from py2neo.types import remote
class GraphObjectWithRelProps(GraphObject):
"""Simple extension to add get method for a relationship
between self and another node"""
@staticmethod
def _get_relationship(source, target, relationship):
"""Returns the relationship connecting source and target as set
in the class definition.
Copy this method, adding an argument for the params
and altering the cypher to add a set function
Only works if two classes are rleated by only one relationship
:param source: GraphObject instance - the starting node
:param target: GraphObject instance - the ending node
:param relationship: str name of the relationship on source"""
# get relationship pattern
rel = getattr(source, relationship)
# the pattern is calculated for us on this object
pattern = rel._RelatedObjects__relationship_pattern
# basic cypher query
q = ('MATCH {0} '
'WHERE id(a) = $sId AND id(b) = $tId '
'RETURN _').format(pattern)
# for a set operation you would add
# DELETE _ CREATE {0} SET _ += $relParams
# where $relParams is supplied as a dict with your params
# you could also add a patch method using _ += $relParams
# the remote function allows us to get a reference to the graph
graph = remote(source.__ogm__.node).graph
# as well as the node IDs of the GraphObject
params = {
'sId': remote(source.__ogm__.node)._id,
'tId': remote(target.__ogm__.node)._id,
}
return graph.run(q, params).evaluate()
class Intent(GraphObjectWithRelProps):
name = Property()
message = RelatedTo("Message", "HAS")
def get_message_rel(self, n):
graph = remote(self.__ogm__.node).graph
return self._get_relationship(graph, self, n, 'message')
class Message(GraphObjectWithRelProps):
name = Property()
owner = RelatedFrom("Intent","HAS")
def get_owner_rel(self, n):
graph = remote(self.__ogm__.node).graph
return self._get_relationship(graph, self, n, 'owner')