gpt4 book ai didi

django - 获取 django.db.utils.IntegrityError : duplicate key value violates unique constraint But value does not exists

转载 作者:行者123 更新时间:2023-12-03 23:03:10 24 4
gpt4 key购买 nike

我在 Django 中使用 PostgreSQL,我试图使用 Django ORM 从 DB 获取一些对象。

Medicine.objects.get(unique_item_id=26775)
但是在获取时我遇到了 Error -> item_medicine.models.DoesNotExist: Medicine matching query does not exist.然后我尝试使用 Django ORM 插入相同的内容。
Medicine.objects.create(unique_item_id=26775)
但我再次收到错误 psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "item_medicine_medicine_unique_item_id_key" DETAIL: Key (unique_item_id)=(26775) already exists.在我的模型中,我添加了 unique=Trueunique_item_id field 。
我不知道为什么会这样。我尝试了类似帖子中给出的答案,但没有任何效果。
追溯:
Traceback (most recent call last):
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "item_medicine_medicine_unique_item_id_key"
DETAIL: Key (unique_item_id)=(26775) already exists.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-46fdec6a582b>", line 1, in <module>
Medicine.objects.create(unique_item_id=26775)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/home/rohit/Projects/medicine/item_medicine/models.py", line 58, in save
super(Medicine, self).save(*args, **kwargs)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/models/base.py", line 808, in save
force_update=force_update, update_fields=update_fields)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/models/base.py", line 838, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/models/base.py", line 924, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/models/base.py", line 963, in _do_insert
using=using, raw=raw)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1112, in execute_sql
cursor.execute(sql, params)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "item_medicine_medicine_unique_item_id_key"
DETAIL: Key (unique_item_id)=(26775) already exists.
先谢谢各位了!!

最佳答案

如果您在 Medicine.objects 中进行任何类型的过滤那么你很可能会遇到这个问题。例如,假设您定义了以下模型和管理器。

class SoftDeleteManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(deleted_at=None)

class Medecine(models.Model):
...
unique_item_id = models.IntegerField(unique=True)
deleted_at = models.DateTimeField(null=True)

objects = SoftDeleteManager()

conflicting = Medecine.objects.create(unique_item_id=26775, deleted_at=some_datetime)

Medecine.objects.get(unique_item_id=26775) # Results in DoesNotExist
Medecine.objects.create(unique_item_id=26775) # Results in IntegrityError
如果不是这种情况,那么您的 PostgreSQL 索引可能已损坏。
我建议你尝试运行 the REINDEX command在你的 table 上 REINDEX TABLE item_medicine_medicine

关于django - 获取 django.db.utils.IntegrityError : duplicate key value violates unique constraint But value does not exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64293005/

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