neo4j-contrib / py2neo

EOL! Py2neo is a comprehensive Neo4j driver library and toolkit for Python.
https://py2neo.org
Apache License 2.0
20 stars 8 forks source link

Non Primary Key get on Repositories... vs. graph.nodes.match can it return a Model? #883

Closed iamliamc closed 3 years ago

iamliamc commented 3 years ago

Here is my Model:

class Register(Model):
    __primarylabel__ = "Register"
    __primarykey__ = "uuid"

    uuid = Property()
    point_id = Property()
    brick_class = Property()

    def __init__(self, uuid: str, point_id: str, brick_class: str):
        self.uuid = uuid
        self.point_id = point_id
        self.brick_class = brick_class

This gives me the behavior that I want only I want it with a property other than the primary key: https://py2neo.org/2021.0/ogm/index.html?highlight=get#py2neo.ogm.Repository.get

>>> from py2neo.ogm import Repository
>>> from building_topology.adapters.secondary.persistance_graphql.models import Register
>>> repo = Repository()
>>> repo.get(Register, "76c46a70-6768-47dc-87b4-ac6f7860f227")
<Register uuid='76c46a70-6768-47dc-87b4-ac6f7860f227'>

Is there anyway to query the non-primary key and get the Register not the Node back?

>>> graph.nodes.match("Register", point_id="KRSD8ADM.Air Distribution.AHU.OAT X1").first()
Node('Outside Air Temperature Low Reset Setpoint', 'Register', brick_class='Outside Air Temperature Low Reset Setpoint', point_id='KRSD8ADM.Air Distribution.AHU.OAT X1', uuid='76c46a70-6768-47dc-87b4-ac6f7860f227')

I guess perhaps this is a bit of a push back on if i'm using the right primary key... but I do think I need both...

technige commented 3 years ago

You can use matching on a model as well: https://py2neo.org/2021.0/ogm/index.html#object-matching

iamliamc commented 3 years ago

Very cool thanks for... knowing the docs:

Register.match(self.graph).where(point_id=point_id).first()
<Register uuid='76c46a70-6768-47dc-87b4-ac6f7860f227'>
self.graph.nodes.match("Register", point_id=point_id).first()
Node('Outside Air Temperature Low Reset Setpoint', 'Register', brick_class='Outside Air Temperature Low Reset Setpoint', point_id='KRSD8ADM.Air Distribution.AHU.OAT X1', uuid='76c46a70-6768-47dc-87b4-ac6f7860f227')