Search code examples
pythonflaskneo4jcypherpy2neo

Modify or change relationship properties in py2neo


Is there a way to change a relationship property once it has been set using py2neo or cypher? I'm creating an inventory tracker and once an item is "CHECKED_OUT" a property called "status" in the relationship is set to "True". Ideally, once the item is returned or checked in, I'd like to change the "status" property to "False". This way I can keep track of the item and prevent it from being checked out twice.

Here is my code for creating the relationship for a checkout transaction:

    def check_out(self, barcode):
        emp = self
        item = barcode
        id = str(uuid.uuid4())
        timestamp = int(datetime.now().strftime("%s"))
        date = datetime.now().strftime("%F")
        status=True
        rel = Relationship(emp, "CHECKED_OUT", item, id=id, timestamp=timestamp, date=date, status=status)
        if not(graph.create_unique(rel)):
            return True
        else:
            return False

I've read through the py2neo API and I can't seem to find the answer. If modifying the relationship is the wrong approach, can you offer a better one?


Solution

  • Something along this line should work:

    def check_in(self, barcode):
        item = barcode
    
        # get the relationship
        for rel in graph.match(start_node=self, rel_type="CHECKED_OUT", end_node=item):
            # set property
            rel.properties["status"] = False
            rel.push()
    

    See match(): http://py2neo.org/2.0/essentials.html#py2neo.Graph.match

    and properties: http://py2neo.org/2.0/essentials.html#py2neo.Relationship.properties