Database errors in the shell should roll back the transaction
|Reported by:||Glenn Maynard||Owned by:||Aymeric Augustin|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||fnl, diegobz||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
If an SQL statement fails in Postgresql, the connection refuses to run any further SQL commands until the transaction is rolled back. In normal operation, this is fine; but in the shell, it's a constant aggrevation.
This is related to #852, but that ticket was asking to automatically rollback inside the connection itself, which isn't good. This ticket is only regarding the shell. If I run a function that causes an SQL error, the shell's connection should not be left in an unusable state, forcing me to manually import db.transaction and roll back a transaction I never asked for. The shell should catch DatabaseError and rollback before returning to the console.
The attached patch implements this. I've only tested with Postgresql. This does not affect IPython (which I know nothing about).
>>> obj = User(name="Bill") >>> obj.save() ... IntegrityError: duplicate key value violates unique constraint "app_user_name_key" >>> obj.name = "Jim" >>> obj.save() ... InternalError: current transaction is aborted, commands ignored until end of transaction block
>>> obj = User(name="Bill") >>> obj.save() ... IntegrityError: duplicate key value violates unique constraint "app_user_name_key" >>> obj.name = "Jim" >>> obj.save() >>>
Change History (15)
comment:1 Changed 7 years ago by
|Patch needs improvement:||unset|
|Triage Stage:||Unreviewed → Design decision needed|
comment:6 Changed 5 years ago by
|Patch needs improvement:||set|
|Triage Stage:||Design decision needed → Accepted|
comment:13 Changed 4 years ago by
|Owner:||changed from nobody to Aymeric Augustin|
|Status:||new → assigned|