I have a basic conceptual question regarding using ORMs (Peewee specifically) on dynamically changing relational data.
Peewee maps information from a database to a Python object for ease of use.
My understanding is that the query produces the Python object at the time of the query. Should the DB data then be altered somehow (via another thread, in my case) the generated object's properties are now out of date, correct?
My core issue is that my app is very asynchronous in structure -- a webapp provides the front end (via Flask), hardware sensors input data, and running daemons determine actions to take based on the contents of my database. As such I am having to manually requery constantly so that my Python objects properly represent the data in the database, e.g.:
name = 'obj_name'
instance = Model.select().where(Model.name = name)
#time passes, need to refresh
instance = Model.get(Model.id == instance.id)
Is there some way to make this more elegant?
Ideally I would automatically requery the database every time the Peewee object is accessed.
Alternately I could add a refresh() method that updates the object e.g.:
instance = instance.refresh()
Any suggestions?
What's the cleanest way to work with an ORM in this type of situation?
So you have long-lived Python objects in one thread, meanwhile another thread is updating the database? Re-querying is the best you can do with an ActiveRecord ORM like Peewee.
def refresh(self):
return type(self).get_by_id(self._pk)