fix: QueryBuilder预加载列避免detached对象lazy loading
- 在QueryBuilder.first()和all()中预加载所有列 - 防止在session外访问属性导致greenlet_spawn错误 - 与CRUD层修复保持一致的模式
This commit is contained in:
@@ -204,6 +204,14 @@ class QueryBuilder(Generic[T]):
|
||||
result = await session.execute(self._stmt)
|
||||
instances = list(result.scalars().all())
|
||||
|
||||
# 预加载所有列以避免detached对象的lazy loading问题
|
||||
for instance in instances:
|
||||
for column in self.model.__table__.columns:
|
||||
try:
|
||||
getattr(instance, column.name)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 写入缓存
|
||||
if self._use_cache:
|
||||
cache = await get_cache()
|
||||
@@ -232,6 +240,14 @@ class QueryBuilder(Generic[T]):
|
||||
result = await session.execute(self._stmt)
|
||||
instance = result.scalars().first()
|
||||
|
||||
# 预加载所有列以避免detached对象的lazy loading问题
|
||||
if instance is not None:
|
||||
for column in self.model.__table__.columns:
|
||||
try:
|
||||
getattr(instance, column.name)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 写入缓存
|
||||
if instance is not None and self._use_cache:
|
||||
cache = await get_cache()
|
||||
|
||||
Reference in New Issue
Block a user