I want to get computed field in returning after updating (sqlalchemy)

Simple sample:

from sqlalchemy import Column, Integer, Computed, update, insert, select
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.orm import DeclarativeBase, declared_attr


engine = create_async_engine(url="postgresql+asyncpg://db_user:db_pass@localhost:54320/db_name")
async_session = async_sessionmaker(bind=engine)


class BaseTable(DeclarativeBase):
    @declared_attr
    def __tablename__(cls) -> str:
        return cls.__name__.lower()


class Tablichka(BaseTable):
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    quantity = Column(Integer, nullable=False)
    price = Column(Integer, nullable=False)
    total_cost = Column(Integer, Computed("quantity * price"))  # Attention!

I want to update the row using an orm query. I need to get a result, so I use returning:

async def run():
    async with async_session() as session:
        statement = insert(Tablichka).values(quantity=2, price=100).returning(Tablichka)
        obj = (await session.execute(statement)).scalar_one()
        print(f"total_cost: wait 200, really {obj.total_cost}")  # total_cost: wait 200, really 200
        id = obj.id

        statement = update(Tablichka).filter_by(id=id).values(quantity=4, price=100).returning(Tablichka)
        obj = (await session.execute(statement)).scalar_one()
        print(f"total_cost: wait 400, really {obj.total_cost}")   # total_cost: wait 400, really 200

        statement = select(Tablichka).filter_by(id=id)
        obj = (await session.execute(statement)).scalar_one()
        print(f"total_cost: wait 400, really {obj.total_cost}")   # total_cost: wait 400, really 200

        await session.commit()

    async with async_session() as session:
        statement = select(Tablichka).filter_by(id=id)
        obj = (await session.execute(statement)).scalar_one()
        print(f"ntotal_cost: wait 400, really {obj.total_cost}")  # total_cost: wait 400, really 400


if __name__ == "__main__":
    import asyncio

    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()

After the update, I expect to get a recalculated total_cost, but I get an old total_cost. I get updated fields except calculated fields. At the same time, the value is recalculated in the database. This confirms the select query in the new session.
How do I solve the problem?

Actually, I have a working example. But I don’t have an understanding of why it works:

async def run_create(session: AsyncSession):
    statement = insert(Tablichka).values(quantity=2, price=100).returning(Tablichka)
    obj = (await session.execute(statement)).scalar_one()
    print(f"total_cost: wait 200, really {obj.total_cost}")  # total_cost: wait 200, really 200
    return obj.id


async def run_update(session: AsyncSession, id: int):
    statement = update(Tablichka).filter_by(id=id).values(quantity=4, price=100).returning(Tablichka)
    obj = (await session.execute(statement)).scalar_one()
    print(f"total_cost: wait 400, really {obj.total_cost}")  # total_cost: wait 400, really 400


async def run():
    async with async_session() as session:
        id = await run_create(session)
        await run_update(session, id)


if __name__ == "__main__":
    import asyncio

    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()

But in my project there is a more complex example in which I cannot use my working example. I need to fix my broken example.
I use the following versions:

python = "^3.10"
sqlalchemy = "^2.0.35"
asyncpg = "^0.29.0"
greenlet = "^3.1.0"

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

I want to get computed field in returning after updating (sqlalchemy)

Simple sample:

from sqlalchemy import Column, Integer, Computed, update, insert, select
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.orm import DeclarativeBase, declared_attr


engine = create_async_engine(url="postgresql+asyncpg://db_user:db_pass@localhost:54320/db_name")
async_session = async_sessionmaker(bind=engine)


class BaseTable(DeclarativeBase):
    @declared_attr
    def __tablename__(cls) -> str:
        return cls.__name__.lower()


class Tablichka(BaseTable):
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    quantity = Column(Integer, nullable=False)
    price = Column(Integer, nullable=False)
    total_cost = Column(Integer, Computed("quantity * price"))  # Attention!

I want to update the row using an orm query. I need to get a result, so I use returning:

async def run():
    async with async_session() as session:
        statement = insert(Tablichka).values(quantity=2, price=100).returning(Tablichka)
        obj = (await session.execute(statement)).scalar_one()
        print(f"total_cost: wait 200, really {obj.total_cost}")  # total_cost: wait 200, really 200
        id = obj.id

        statement = update(Tablichka).filter_by(id=id).values(quantity=4, price=100).returning(Tablichka)
        obj = (await session.execute(statement)).scalar_one()
        print(f"total_cost: wait 400, really {obj.total_cost}")   # total_cost: wait 400, really 200

        statement = select(Tablichka).filter_by(id=id)
        obj = (await session.execute(statement)).scalar_one()
        print(f"total_cost: wait 400, really {obj.total_cost}")   # total_cost: wait 400, really 200

        await session.commit()

    async with async_session() as session:
        statement = select(Tablichka).filter_by(id=id)
        obj = (await session.execute(statement)).scalar_one()
        print(f"ntotal_cost: wait 400, really {obj.total_cost}")  # total_cost: wait 400, really 400


if __name__ == "__main__":
    import asyncio

    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()

After the update, I expect to get a recalculated total_cost, but I get an old total_cost. I get updated fields except calculated fields. At the same time, the value is recalculated in the database. This confirms the select query in the new session.
How do I solve the problem?

Actually, I have a working example. But I don’t have an understanding of why it works:

async def run_create(session: AsyncSession):
    statement = insert(Tablichka).values(quantity=2, price=100).returning(Tablichka)
    obj = (await session.execute(statement)).scalar_one()
    print(f"total_cost: wait 200, really {obj.total_cost}")  # total_cost: wait 200, really 200
    return obj.id


async def run_update(session: AsyncSession, id: int):
    statement = update(Tablichka).filter_by(id=id).values(quantity=4, price=100).returning(Tablichka)
    obj = (await session.execute(statement)).scalar_one()
    print(f"total_cost: wait 400, really {obj.total_cost}")  # total_cost: wait 400, really 400


async def run():
    async with async_session() as session:
        id = await run_create(session)
        await run_update(session, id)


if __name__ == "__main__":
    import asyncio

    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()

But in my project there is a more complex example in which I cannot use my working example. I need to fix my broken example.
I use the following versions:

python = "^3.10"
sqlalchemy = "^2.0.35"
asyncpg = "^0.29.0"
greenlet = "^3.1.0"

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

I want to get computed field in returning after updating (sqlalchemy)

Simple sample:

from sqlalchemy import Column, Integer, Computed, update, insert, select
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.orm import DeclarativeBase, declared_attr


engine = create_async_engine(url="postgresql+asyncpg://db_user:db_pass@localhost:54320/db_name")
async_session = async_sessionmaker(bind=engine)


class BaseTable(DeclarativeBase):
    @declared_attr
    def __tablename__(cls) -> str:
        return cls.__name__.lower()


class Tablichka(BaseTable):
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    quantity = Column(Integer, nullable=False)
    price = Column(Integer, nullable=False)
    total_cost = Column(Integer, Computed("quantity * price"))  # Attention!

I want to update the row using an orm query. I need to get a result, so I use returning:

async def run():
    async with async_session() as session:
        statement = insert(Tablichka).values(quantity=2, price=100).returning(Tablichka)
        obj = (await session.execute(statement)).scalar_one()
        print(f"total_cost: wait 200, really {obj.total_cost}")  # total_cost: wait 200, really 200
        id = obj.id

        statement = update(Tablichka).filter_by(id=id).values(quantity=4, price=100).returning(Tablichka)
        obj = (await session.execute(statement)).scalar_one()
        print(f"total_cost: wait 400, really {obj.total_cost}")   # total_cost: wait 400, really 200

        statement = select(Tablichka).filter_by(id=id)
        obj = (await session.execute(statement)).scalar_one()
        print(f"total_cost: wait 400, really {obj.total_cost}")   # total_cost: wait 400, really 200

        await session.commit()

    async with async_session() as session:
        statement = select(Tablichka).filter_by(id=id)
        obj = (await session.execute(statement)).scalar_one()
        print(f"ntotal_cost: wait 400, really {obj.total_cost}")  # total_cost: wait 400, really 400


if __name__ == "__main__":
    import asyncio

    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()

After the update, I expect to get a recalculated total_cost, but I get an old total_cost. I get updated fields except calculated fields. At the same time, the value is recalculated in the database. This confirms the select query in the new session.
How do I solve the problem?

Actually, I have a working example. But I don’t have an understanding of why it works:

async def run_create(session: AsyncSession):
    statement = insert(Tablichka).values(quantity=2, price=100).returning(Tablichka)
    obj = (await session.execute(statement)).scalar_one()
    print(f"total_cost: wait 200, really {obj.total_cost}")  # total_cost: wait 200, really 200
    return obj.id


async def run_update(session: AsyncSession, id: int):
    statement = update(Tablichka).filter_by(id=id).values(quantity=4, price=100).returning(Tablichka)
    obj = (await session.execute(statement)).scalar_one()
    print(f"total_cost: wait 400, really {obj.total_cost}")  # total_cost: wait 400, really 400


async def run():
    async with async_session() as session:
        id = await run_create(session)
        await run_update(session, id)


if __name__ == "__main__":
    import asyncio

    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()

But in my project there is a more complex example in which I cannot use my working example. I need to fix my broken example.
I use the following versions:

python = "^3.10"
sqlalchemy = "^2.0.35"
asyncpg = "^0.29.0"
greenlet = "^3.1.0"

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật