django-debug-toolbar/django-debug-toolbar — Issue #230: Crashed transactioned PostgreSQL INSERT's are not logged
Author: IlyaSemenov
Created: 2011-11-14 08:43:34+00:00
State: open

Steps to reproduce:

1. Create a database with django.db.backends.postgresql_psycopg2 backend
2. Add django.middleware.transaction.TransactionMiddleware to MIDDLEWARE_CLASSES
3. Run a crashing INSERT query somehow

Result:
4. The crashed INSERT query is not logged, and django debug toolbar exception replaces the actual exception.

Expected result:
4. The crashed INSERT query is logged as usual

Example:
```
Traceback (most recent call last):
File "/home/ubuntu/minisites/src/thumbnails/models.py", line 106, in for_file
thumbnail.save()
File "/home/ubuntu/minisites/externals/django/django/db/models/base.py", line 464, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/ubuntu/minisites/externals/django/django/db/models/base.py", line 551, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/ubuntu/minisites/externals/django/django/db/models/manager.py", line 203, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home/ubuntu/minisites/externals/django/django/db/models/query.py", line 1556, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/ubuntu/minisites/externals/django/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/home/ubuntu/minisites/externals/django-debug-toolbar/debug_toolbar/utils/tracking/db.py", line 128, in execute
'iso_level': conn.isolation_level,
InternalError: current transaction is aborted, commands ignored until end of transaction block
```

If iso_level fetching is commented out, the query is logged successfully, and the actual exception is propagated.

```diff
diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py
index 7370374..3718c88 100644
--- a/debug_toolbar/utils/tracking/db.py
+++ b/debug_toolbar/utils/tracking/db.py
@@ -125,7 +125,7 @@ class NormalCursorWrapper(object):
params.update({
'trans_id': self.logger.get_transaction_id(alias),
'trans_status': conn.get_transaction_status(),
- 'iso_level': conn.isolation_level,
+# 'iso_level': conn.isolation_level,
'encoding': conn.encoding,
})
```

```
Traceback (most recent call last):
File "/home/ubuntu/minisites/src/thumbnails/models.py", line 106, in for_file
thumbnail.save()
File "/home/ubuntu/minisites/externals/django/django/db/models/base.py", line 464, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/ubuntu/minisites/externals/django/django/db/models/base.py", line 551, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/ubuntu/minisites/externals/django/django/db/models/manager.py", line 203, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home/ubuntu/minisites/externals/django/django/db/models/query.py", line 1556, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/ubuntu/minisites/externals/django/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/home/ubuntu/minisites/externals/django-debug-toolbar/debug_toolbar/utils/tracking/db.py", line 70, in execute
return self.cursor.execute(sql, params)
File "/home/ubuntu/minisites/externals/django/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "/home/ubuntu/minisites/externals/django/django/db/backends/postgresql_psycopg2/base.py", line 46, in execute
return self.cursor.execute(query, args)
DatabaseError: cannot perform INSERT RETURNING on relation "thumbnails_thumbnail"
HINT: You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.
```