In the following code snippet a commit is executed once the with block is exited. As per the docs https://docs.sqlalchemy.org/en/20/orm/session_api.html#sqlalchemy.orm.Session.begin The Session object features autobegin behavior, so that normally it is not necessary to call the Session.begin() method explicitly Running without begin, like so does not commit even though the docs mention autobegin is not needed. The docs for Session.begin
Tag: transactions
How to move a document between 2 collections in Firebase?
I’m trying to “move” a document from one collection to another in Firestore. That means copy/paste a document and then delete the original. I’m trying to achieve this server-side using Python. I’m aware that using transactions might do the trick, but I’m wondering why couldn’t I just use a basic : .get() .set() and .delete() in order to do that?
Python flask-sqlalchemy: Do I have to commit session after a query?
I am writing an app in python flask-sqlalchemy with MySQL DB (https://flask-sqlalchemy.palletsprojects.com/en/2.x/) and I am wondering if I have to make “db.session.commit() or db.session.rollback()” after GET call, which only query DB . For example: Answer orders = Order.query.all() is a SELECT query that could be extended to include additional filters (WHERE etc.). It doesn’t alter the database, it simply reads
How to test a Django on_commit hook without clearing the database?
The on_commit function has been added to Django 1.9 to be able to trigger an action (e.g. a Celery task) after the current transaction has committed. They mention later in the docs that one should use TransactionTestCase to test features that rely on that function. However, unlike TestCase (which uses transactions and rolls them back), TransactionTestCase empties the whole database
Creating transactions with with statements in psycopg2
I am trying to use psycopg2 to add some new columns to a table. PostgreSQL lacks a ALTER TABLE table ADD COLUMN IF NOT EXISTS, so I am adding each column in it’s own transaction. If the column exists, there will be a python & postgres error, that’s OK, I want my programme to just continue and try to add
What is the simplest way to retry a Django view upon an exception being raised?
I want to rerun a Django view function if a certain exception is raised (in my case a serialization error in the underlying database). I want it to run with exactly the same parameters, including the same request object – as if the client had re-requested the URL. There are many database queries in the view, and the exception could
Atomic increment of a counter in django
I’m trying to atomically increment a simple counter in Django. My code looks like this: If I understand Django correctly, this should wrap the function in a transaction and make the increment atomic. But it doesn’t work and there is a race condition in the counter update. How can this code be made thread-safe? Answer Use an F expression: either