从 SQLObject 迁移到 SQLAlchemy :: SQLAlchemy 可以支持零缓存和自动加载表更新吗?
我想知道当另一个进程更新同一对象的某些属性时,SQLAlchemy 是否可以支持从数据库即时加载对象的当前状态。
我想提一下,我正在从 SQLObject 迁移到 SQLAlchemy。使用 SQLObject,目前,我有一个表,如下所示:
class MyConfiguration(SQLObject):
class sqlmeta:
cacheValues = False
attr1 = StringCol(default='')
attr2 = IntCol(default=0)
如您所知,此 cacheValues=False 设置立即将属性更新刷新到磁盘:
global my_config
my_config = MyConfiguration()
my_config.attr1 = 'some value'
即时 attr1 设置为“某个值”,该值可供使用同一对象的另一个进程使用我的配置。
我已经完成了 SQLAlchemy 的 ORM 和 Core 的基本教程,据我所知,我能实现的最接近的方法是使用 session.merge() 因为除非我添加或合并,否则 SQLAlchemy 中的“my_config”状态将不会变为挂起/脏并且 autoflush=True 时,后续查询将重新从表中读取该行。
我认为的另一个选择是我可以覆盖 MyConfiguration 中的 setattr 以立即刷新对实例的更改。然而,我不喜欢这个,因为这很丑陋,而且我想尽可能多地使用 SQLAlchemy 的功能,而不是我去黑一些东西。
所以我试图在 SQLAlchemy 中找到这个 SQLObject 功能的完全匹配。这可能吗?
预先感谢您的帮助。
I want to know if SQLAlchemy can support instant loading of the current state of an object from the database when another process updates some attributes of the same object.
I want to mention, I am migrating from SQLObject to SQLAlchemy. Using SQLObject, currently, I have one table like:
class MyConfiguration(SQLObject):
class sqlmeta:
cacheValues = False
attr1 = StringCol(default='')
attr2 = IntCol(default=0)
As you know, this cacheValues=False setting instantly flushes attribute updates to the disk:
global my_config
my_config = MyConfiguration()
my_config.attr1 = 'some value'
The instant attr1 is set to 'some value', that value is available to another process using the same object my_config.
I have gone through the basic tutorial's of SQLAlchemy's ORM and Core and as I understand, the nearest I can go to achieve this is by using session.merge() because unless I add or merge, the state of "my_config" in SQLAlchemy will not become pending/dirty and with autoflush=True, the subsequent querying will re-read the row from the table.
Another option I thought was I could over-ride setattr in MyConfiguration to flush changes to an instance instantly. However, I don't like this because this is ugly and I want to use as much SQLAlchemy's features as possible than me hacking something.
So I am trying to find an exact match of this SQLObject's feature in SQLAlchemy. Is this possible?
Thanks in advance for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论