Search code examples
pythonsqlalchemyfastapi

Error while writing an asynchronous function for deletion on FASTAPI


The function should delete an item (Bookings) taking room_id and user_id.In the terminal it says "DELETE /bookings/10 HTTP/1.1" 200 OK, but nothing is deleted from the database. I am new in this business

 @classmethod
    async def remove(cls, user_id: int, room_id: int):
            async with async_session_maker() as session:
                query = select(Rooms).filter_by(id=room_id)
                result = await session.execute(query)
                result = result.scalar_one_or_none()
                if result is None:
                    raise {"message": "Room not found"}

                delete_query = delete(Bookings).filter_by(room_id=room_id, user_id=user_id)
                print(delete_query)
                await session.execute(delete_query)
@router.delete("/{id}")
async def remove_booking(
        id: int,
        user: Users = Depends(get_current_user),
):
    await BookingDAO.remove(user.id, id)

Honestly, I can't think of anything that could be wrong.


Solution

  • I think you are just missing a commit after you execute the delete.

    Try this:

    await session.execute(delete_query)
    await session.commit()