Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you rollback during Django shell session after causing DatabaseError?

Tags:

django

I used to know how to do this, but I forgot. Sometimes, while using the Django shell, you make a mistake and cause the transaction to become aborted. After that, any further queries you do will result in DatabaseError: current transaction is aborted, commands ignored until end of transaction block. However, I think there is a way to rollback the transaction when this happens manually so you don't have to restart your session. Can anyone tell me what it is?

This bug report (https://code.djangoproject.com/ticket/10813) makes reference to the technique but doesn't explain it. Running django.db.transaction.rollback results in "TransactionManagementError: This code isn't under transaction management".

like image 651
darth happyface Avatar asked Sep 02 '11 18:09

darth happyface


1 Answers

This happens to me all the time when using Postgres, and it's really irritating.

You want:

from django.db import transaction
transaction.rollback()

Most of the time this is fine (and in my experience, it's safe to ignore the TransactionManagementError).

like image 140
Seth Avatar answered Sep 23 '22 02:09

Seth