When I use 'await session.commit()', the field content of my instance has become an error #1258
-
First Check
Commit to Help
Example Codeimport uuid
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.db import async_engine
from sqlmodel import SQLModel, Field
class Test(SQLModel, table=True):
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
name: str = Field(max_length=255)
async def main():
async with async_engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
async with AsyncSession(async_engine) as session:
test = Test(name='test')
session.add(test)
await session.commit()
await session.refresh(test)
return test
if __name__ == '__main__':
import asyncio
asyncio.run(main()) DescriptionWhen I use 'await session.commit()', the field content of my instance has become an error, like Operating SystemWindows Operating System DetailsNo response SQLModel Version0.0.22 Python Version3.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is a sqlalchemy issue. Try this |
Beta Was this translation helpful? Give feedback.
-
Your code works well SQLAlchemy marks object as expired after calling So, after |
Beta Was this translation helpful? Give feedback.
Your code works well
SQLAlchemy marks object as expired after calling
commit
.Next time you are attempting to use them it will try to refresh that object.
But it will fail since you are using async connection and all calls should be awaited.
So, after
commit
you shouldrefresh
objects to use them later