Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7760485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:54:01+00:00 2026-06-01T13:54:01+00:00

I am using sql alchemy in my project, I used db session, engine =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:54:02+00:00Added an answer on June 1, 2026 at 1:54 pm

    Nested transaction, as suggested by Lafada, is not intended for such situation. A flush() will do just fine, e.g.

    db_session.begin()
    try:
        db_session.add(order)
    
        db_session.flush()
    
        order_line_item.id = order.id
        db_session.add(order_line_item)
    
        db_session.commit()
    except:
        db_session.rollback()
    

    Or better still, if you have relationship setup properly between order and line item, you shouldn’t even bother to assign id manually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQlAlchemy in my web project. What should I use - scoped_session(sessionmaker())
When using SQL Alchemy for abstracting your data access layer and using controllers as
Using SQLAlchemy, an Engine object is created like this: from sqlalchemy import create_engine engine
I am trying to use Elixir with Flask. Reason being using SQL Alchemy directly
I have been working on a website using mod_python, python, and SQL Alchemy when
I am using SQL Alchemy and I want to return a list of Document
I am connecting to a MS SQL server through SQL Alchemy, using pyodbc module.
I'm using SQLalchemy for a Python project, and I want to have a tidy
I'm using the SQLAlchemy Python ORM in a Pylons project. I have a class
Hi im using sqlalchemy on a db2 table with 500k rows. using plain sql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.