Cause of Django duplicate entry error (1062)?

蹲街弑〆低调 提交于 2019-12-10 11:27:45

问题


I updated the info below to reference a different model/view that is giving me the same error. Its a simpler model so there are less variables to take into account...

I have the following model:

class Imaging_order(Order):
    order_description = models.ForeignKey(Imaging_test, limit_choices_to = {'active': 1}, null=True, blank=True)
    orders = models.ManyToManyField(Imaging_test, limit_choices_to = {'active': 1}, related_name='orders')
    ...

I have the following views (basically the generic django class based update view with a couple custom functions mixed in):

class LoginRequiredMixin(object):
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

class EditMixin(object):
    def form_valid(self, form):
        messages.success(self.request, "This %s has been updated." % (self.model._meta.verbose_name), extra_tags='msg')
        return super(EditMixin, self).form_valid(form)

class ImagingMixin(LoginRequiredMixin):
    model = Imaging_order
    form_class = ImagingForm

class ImagingUpdateView(ImagingMixin, EditMixin, UpdateView):
    pass

On our live version I occassionally get notices of this error:

    Traceback (most recent call last):

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/base.py", line 47, in view
    return self.dispatch(request, *args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/utils/decorators.py", line 28, in _wrapper
    return bound_func(*args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/utils/decorators.py", line 24, in bound_func
    return func(self, *args2, **kwargs2)

  File "/home/cpcadmin/webapps/cpcdj2/cpc/emr/views.py", line 150, in dispatch
    return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/base.py", line 68, in dispatch
    return handler(request, *args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/edit.py", line 195, in post
    return super(BaseUpdateView, self).post(request, *args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/edit.py", line 138, in post
    return self.form_valid(form)

  File "/home/cpcadmin/webapps/cpcdj2/cpc/emr/views.py", line 155, in form_valid
    return super(EditMixin, self).form_valid(form)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/edit.py", line 112, in form_valid
    self.object = form.save()

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/forms/models.py", line 363, in save
    fail_message, commit, construct=False)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/forms/models.py", line 86, in save_instance
    save_m2m()

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/forms/models.py", line 82, in save_m2m
    f.save_form_data(instance, cleaned_data[f.name])

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 1149, in save_form_data
    setattr(instance, self.attname, data)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 746, in __set__
    manager.add(*value)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 503, in add
    self._add_items(self.source_field_name, self.target_field_name, *objs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 587, in _add_items
    '%s_id' % target_field_name: obj_id,

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/query.py", line 360, in create
    obj.save(force_insert=True, using=self.db)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/base.py", line 460, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/base.py", line 553, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/query.py", line 1436, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/sql/compiler.py", line 791, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)

  File "/usr/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)

  File "/usr/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue

IntegrityError: (1062, "Duplicate entry '25129-18' for key 2")

I cannot tell when this error is occurring (usually this view works just fine). I think I have a normal model/view/form setup except that I extended the generic class-based views a bit. I agree with the below comment that this is related to a m2m field. Every time this error comes up it is calling "save_m2m", which then results in this error. I still can't reproduce it. It occurs from my update page/view so I think its only when updating an existing model with existing related records. This is the closest other post I could find, but was of no help: IntegrityError: (1062, "Duplicate entry '1830327-1792993' for key 'some_instance_A_id'") but no UNIQUE constraint

来源:https://stackoverflow.com/questions/19303866/cause-of-django-duplicate-entry-error-1062

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!