【发布时间】:2021-12-03 20:50:51
【问题描述】:
我正在使用 sqlalchemy + asyncpg,并“选择”预加载。
我的 Person 项目与 Friends 具有一对多关系。
我将一个 Person 插入到我的数据库中,但没有相关的 Friend 条目。如果在同一个会话中我尝试从数据库中获取该 Person,我可以正常访问其静态(非关系)列,但无法访问 friends 关系。
我认为尝试访问person.friends 会触发延迟加载,尽管之前它是作为selectin 加载执行的。为什么是这样?如何避免?
# Create the ORM model
class Person(Base):
__tablename__ = 'items'
id_ = Column(POSTGRES_UUID(as_uuid=True), primary_key=True)
name = Column(String(32))
friends = relationship('Friend', lazy='selectin')
# Create an instance
person_id = uuid4()
person = Person(id_=person_id, name='Alice') # Note that this Person's friends are not set
# Add to database
async with AsyncSession(engine, expire_on_commit=False) as session:
try:
session.begin()
session.add(person)
await session.commit()
except:
await session.rollback()
raise
# Get the added person from the database
created_person = await session.get(person, person_id)
print(created_person.id_) # Works fine
print(created_person.friends) # Raises error
错误:
sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_() here.
Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/14/xd2s)
【问题讨论】:
标签: asynchronous sqlalchemy python-asyncio asyncpg