Modify or change relationship properties in py2neo

2019-06-03 05:28发布

问题:

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?

回答1:

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



回答2:

Thank you so much for this. It worked however, it updates all of the relationships between the item and the person. I modified your response a little to make sure I update the right relationship. Again thank you. I have included the update version below.

    def check_in(self, barcode, id):
    item = barcode

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