开发者

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

开发者 https://www.devze.com 2023-04-01 21:30 出处:网络
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 i

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 repor开发者_如何学编程t (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".


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).


This is slightly better because you get no stack trace about TransactionManagementError:

from django.db import transaction
transaction.rollback_unless_managed()
0

精彩评论

暂无评论...
验证码 换一张
取 消