0

My current solution involves using the execute() method on sqlalchemy each time when I want to run multiple queries. for eg.

connection.connect().execute("query1")
connection.connect().execute("query2")
connection.connect().execute("query3")

This will execute the queries one by one, however it is possible that "query2" execution failed but the "query1" would have ran. how to revert back to original state in case any intermediary queries fail. Like if "query1" and "query2" ran but "query3" failed, then how do I make it rollback to original state

1

1 Answer 1

1

As mentioned in the comments, the desired behavior can be achieved by placing your queries inside a transaction.

Read What is a database transaction? for more information. Here is a quote from the most voted answer in that question to help you get started:

A transaction is a unit of work that you want to treat as "a whole." It has to either happen in full or not at all.

See Using Transactions for details on how to implement this. Your code should look something like this:

# starts a transaction
with engine.begin() as connection:
    connection.execute("query1")
    connection.execute("query2")
    connection.execute("query3")
Sign up to request clarification or add additional context in comments.

1 Comment

You may need to use connection.commit() or connection.rollback() (if it fails) at the bottom of the context manager

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.