gpt4 book ai didi

python - Django 错误有时会发生

转载 作者:太空狗 更新时间:2023-10-29 20:24:18 25 4
gpt4 key购买 nike

我现在很迷茫。我在 Django 中运行一个脚本来创建一个假数据库,一遍又一遍地循环相同的代码。当出现以下错误时,我已经创建了超过 2.5k 个对象:

ProgrammingError: autocommit cannot be used inside a transaction

我查了一下,得到了一个显然已修复的错误单,但那是针对以前的版本,我使用的是 Django 1.6。这是完整的回溯:

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

如果前 2500 个对象没有这个问题,为什么我会收到这个错误?

型号:

根据要求,这是 ListenerRequest 的模型,因为它似乎是导致错误的原因:

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)

最佳答案

如果您使用的是 django_postgrespool,当前版本(在撰写本文时)正在吞噬错误并返回一般错误“ProgrammingError:autocommit cannot be used inside a transaction”。

您可以通过暂时注释掉 settings.py 中的以下行来查看实际错误

DATABASES['default']['ENGINE'] = 'django_postgrespool'

有关更多详细信息,请参阅当前 Unresolved 问题:https://github.com/kennethreitz/django-postgrespool/issues/24

关于python - Django 错误有时会发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21174332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com