微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取django.db.utils.IntegrityError:重复的键值违反了唯一约束,但值不存在

如何解决获取django.db.utils.IntegrityError:重复的键值违反了唯一约束,但值不存在

我将Postgresql与Django一起使用,并且尝试使用Django ORM从数据库获取一些对象。

Medicine.objects.get(unique_item_id=26775)

但是在获取时,我陷入了错误-> 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=True字段添加unique_item_id

我不知道为什么会这样。我尝试过类似帖子中给出的答案,但是没有用。

跟踪:

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,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 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/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",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.

谢谢你们!!

解决方法

如果您在outbound_links = M[M == 1].count() mat = [[1] * 10000] * 10000 n = 10000 #len(mat) # for each column for col_index in range(0,n): # count the number of 1s for row_index in range(0,n): if M[row_index][col_index] == 1: mat[row_index][col_index] = 1 / outbound_links[col_index] else: mat[row_index][col_index] = 0 print(mat) 经理中进行任何过滤,则很可能会遇到此问题。例如,假设您定义了以下模型和管理器。

Medicine.objects

如果不是这种情况,则可能是PostgreSQL索引已损坏。

我建议您尝试在桌上运行the REINDEX command

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。