Django IntegrityError:(1062,“重复条目”';1830327-1792993';用于键';某些实例&#A#x27;),但没有唯一约束

Django IntegrityError:(1062,“重复条目”';1830327-1792993';用于键';某些实例&#A#x27;),但没有唯一约束,django,orm,django-models,Django,Orm,Django Models,在向许多字段添加模型实例时,我很少遇到这样的异常,即: some_instance_A.my_m2m.add(some_instance_B)). 它可以工作99/100次。我觉得奇怪的是,破折号-主键是整数 模型字段的定义如下: my_m2m = ManyToManyField(B) 因此,这是最简单的M2M定义 完全回溯(进行少量编辑以保护隐私:): 您可以发布生成此结果的代码片段吗 至少在PARGRIs中,如果您试图在事务中保存一个实例两次,就会发生这种情况。没有什么可以发布的,

在向许多字段添加模型实例时,我很少遇到这样的异常,即:

 some_instance_A.my_m2m.add(some_instance_B)). 
它可以工作99/100次。我觉得奇怪的是,破折号-主键是整数

模型字段的定义如下:

 my_m2m = ManyToManyField(B)
因此,这是最简单的M2M定义

完全回溯(进行少量编辑以保护隐私:):


您可以发布生成此结果的代码片段吗


至少在PARGRIs中,如果您试图在事务中保存一个实例两次,就会发生这种情况。

没有什么可以发布的,它是由我认为是无关代码包围的单行。但是,如果没有人知道这里发生了什么,那么可能不相关的代码也不是那么不相关:)但是,这是低优先级的问题,所以我不会很快解决它。关于您的评论,有问题的行实际上是
transaction.commit()之后的下一行,因此,在一个事务中保存两次绝对不是一个问题。我遇到了与我没有找到解决方案的问题相同(非常恼人)的问题:。你运气好吗?@rsp我转到Postgresql开始享受生活:)
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'")