Python 如何加载所有sqlalchemy orm实例属性,以便安全地删除它们
在sqlalchemy中,是否可以强制加载我的orm实例的所有属性,以便可以安全地删除 我要处理的具体案例是我写了一个装饰师Python 如何加载所有sqlalchemy orm实例属性,以便安全地删除它们,python,sqlalchemy,Python,Sqlalchemy,在sqlalchemy中,是否可以强制加载我的orm实例的所有属性,以便可以安全地删除 我要处理的具体案例是我写了一个装饰师 def auto_session(func): """ Decorate a function that takes a 'session' keyword argument. If a session is provided, this decorator does nothing; if no session is provided,
def auto_session(func):
"""
Decorate a function that takes a 'session' keyword argument. If a
session is provided, this decorator does nothing; if no session is
provided, one will be created an committed.
Before closing the auto-session, ``expunge_all()`` is called. Be aware
that this means subsequent modifications to these object instances will
NOT be reflected in the database.
"""
def inner(dbi, *args, **kwargs):
if "session" in kwargs:
return func(*args, **kwargs)
else:
kwargs["session"] = Session()
ret = func(*args, **kwargs)
kwargs["session"].commit()
kwargs["session"].expunge_all()
kwargs["session"].close()
return ret
return inner
这导致错误实例未绑定到会话;属性刷新操作无法继续
。据我所知,这是由延迟加载的属性或关系引起的,调用方将无法访问这些属性或关系,因为它们在decorator中被删除
我可以想象解决这个问题的一种方法是在调用expunge_all()
之前强制加载。在炼金术中有没有办法做到这一点