how to do rollback in fastapi

169 views Asked by At

i am trying to save 2 record in to database, in try block i have saved those two records :

session.add(rec_1)
await session.commit()
await session.refresh(rec_1)

i need rec_1 id, so i have saved and accessed in rec_2

session.add(rec_2)
await session.commit()
await session.refresh(rec_2)

in except block i have used session.rollback():

if any error occurs after saving the rec_1, from the database rec_1 is not roll backing ,

if any error occurs i don't want to save the any of the records.

please guide me how to handle this situation,

0

There are 0 answers