I am very confused right now. I was running a script in Django to create a fake database, circling through the same code over and over. I had already created over 2.5k objects when I get the following error:
ProgrammingError: autocommit cannot be used inside a transaction
I looked it up and got a bug-ticket that is apparently fixed, but that was for previos versions, I am using Django 1.6. Here is the full traceback:
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
<ipython-input-31-0597bce30f92> in <module>()
----> 1 make_listeners()
<ipython-input-29-5697f122e144> in make_listeners()
28 seed = random.randint(0,len(user_ids_copy)-1)
29 requested_user = user_ids_copy.pop(seed)
---> 30 user.functions.listen_to(requested_user)
31 print i, user.pk, number_seed, original_seed
32 number_seed -= 1
.../models.py in listen_to(self, user_id)
715 except User.DoesNotExist:
716 return 'User DoesNotExist'
--> 717 request = ListenerRequest.objects.get_or_create(user=requesting, user_requested=requester, is_active=True)
718 if not request[1]:
719 if request.is_accepted:
/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/models/manager.pyc in get_or_create(self, **kwargs)
152
153 def get_or_create(self, **kwargs):
--> 154 return self.get_queryset().get_or_create(**kwargs)
155
156 def create(self, **kwargs):
/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/models/query.pyc in get_or_create(self, **kwargs)
378 obj = self.model(**params)
379 with transaction.atomic(using=self.db):
--> 380 obj.save(force_insert=True, using=self.db)
381 return obj, True
382 except DatabaseError:
/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/transaction.pyc in __exit__(self, exc_type, exc_value, traceback)
328 connection.autocommit = True
329 else:
--> 330 connection.set_autocommit(True)
331 # Outermost block exit when autocommit was disabled.
332 elif not connection.savepoint_ids and not connection.commit_on_exit:
/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/backends/__init__.pyc in set_autocommit(self, autocommit)
331 self.validate_no_atomic_block()
332 self.ensure_connection()
--> 333 self._set_autocommit(autocommit)
334 self.autocommit = autocommit
335
/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/backends/postgresql_psycopg2/base.pyc in _set_autocommit(self, autocommit)
171 def _set_autocommit(self, autocommit):
172 if self.psycopg2_version >= (2, 4, 2):
--> 173 self.connection.autocommit = autocommit
174 else:
175 if autocommit:
ProgrammingError: autocommit cannot be used inside a transaction
Why am I getting this error if I didn't have this problem for the first 2500 objects?
as requested, here is the model for ListenerRequest
since that appears to be causing the error:
class ListenerRequest(models.Model):
listener_relationship_id = models.AutoField(primary_key=True)
#user is the person asking to listen
user = models.ForeignKey(User, related_name='requests')
#user_requested is the person being asked to be listen to
user_requested = models.ForeignKey(User, related_name='requested')
date_requested = models.DateTimeField(auto_now_add=True)
date_accepted = models.DateTimeField(null=True,blank=True)
denied_flag = models.BooleanField(default=False)
denied_date = models.DateTimeField(null=True,blank=True)
unlistened_flag = models.BooleanField(default=False)
unlistened_date = models.DateTimeField(null=True,blank=True)
is_active = models.BooleanField(default=True)
is_accepted = models.BooleanField(default=False)
If you're using django_postgrespool, the current version (as of writing) is swallowing errors and returning the generic error "ProgrammingError: autocommit cannot be used inside a transaction".
You can see the actual error by temporarily commenting out the following line in your settings.py
DATABASES['default']['ENGINE'] = 'django_postgrespool'
See this currently open issue for more details: https://github.com/kennethreitz/django-postgrespool/issues/24
Before trying to create the fake database, try the following:
from django.db import connection
curs = connection.cursor()
curs.execute('SET autocommit = 1')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With