我很少得到這樣的異常增加時,模型實例many2many領域,即:IntegrityError:(1062, 「重複條目 '1830327-1792993' 關鍵 'some_instance_A_id'」),但沒有UNIQUE約束
some_instance_A.my_m2m.add(some_instance_B)).
它說99/100倍。看起來很怪,是破折號標誌 - 作爲主鍵是整數..
型號字段定義是這樣的:
my_m2m = ManyToManyField(B)
所以這是最簡單的M2M定義可能
完全回溯(與小編輯保護隱私:)):
File "*******", line 278, in process_request
some_instance_A.my_m2m.add(some_instance_B)
File "/var/lib/python-support/python2.5/django/db/models/fields/related.py", line 490, in add
self._add_items(self.source_field_name, self.target_field_name, *objs)
File "/var/lib/python-support/python2.5/django/db/models/fields/related.py", line 574, in _add_items
'%s_id' % target_field_name: obj_id,
File "/var/lib/python-support/python2.5/django/db/models/query.py", line 352, in create
obj.save(force_insert=True, using=self.db)
File "/var/lib/python-support/python2.5/django/db/models/base.py", line 435, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/var/lib/python-support/python2.5/django/db/models/base.py", line 528, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/var/lib/python-support/python2.5/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/var/lib/python-support/python2.5/django/db/models/query.py", line 1479, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/var/lib/python-support/python2.5/django/db/models/sql/compiler.py", line 783, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/var/lib/python-support/python2.5/django/db/models/sql/compiler.py", line 727, in execute_sql
cursor.execute(sql, params)
File "/var/lib/python-support/python2.5/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/var/lib/python-support/python2.5/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry '1830327-1792993' for key 'some_instance_A_id'")
我有同樣的(非常惱人的)問題,我還沒有找到解決方案:http://stackoverflow.com/questions/19303866/cause-of-django-duplicate-entry-error-1062。你有沒有運氣? – rsp 2013-11-23 15:09:39
@rsp我切換到Postgresql並開始享受生活:) – 2013-11-23 20:05:08