I am using sql alchemy in my project, I used db session,
engine = create_engine(configuration)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
import models
Base.metadata.create_all(bind=engine)
DB session used as:
db_session.merge(order) #order(model) in object
db_session.commit()
Now I want to insert data in to two tables order and order line item, so I need transaction,
as:
1. In first insert I want inserted order’s id to use in second insert query
2. If second insert query failed then first query should be rollback
Try:
#begin transaction/How to begin transaction?
order=db_session.add(order) #insert into order
#is need to commit db_session here as I need inserted orders id
#here actually db_session.commit() needed to get order's id(auto generated)
#if db_session committed here then sql alchemy starts new session
order_line_item.id = order.id
db_session.add(order_line_item) #insert into order line line item
db_session.commit()
#check transaction status if failed then rollback, How to check status?
except:
db_session.rollback()
How to use trasaction?
Nested transaction, as suggested by Lafada, is not intended for such situation. A
flush()will do just fine, e.g.Or better still, if you have relationship setup properly between order and line item, you shouldn’t even bother to assign id manually.