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 |
Answered by
YuriiMotov
Aug 19, 2025
Replies: 2 comments
|
Your code works well SQLAlchemy marks object as expired after calling So, after |
0 replies
Answer selected by
YuriiMotov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
commityou shouldrefreshobjects to use them later