IntegrityError for unique_together ?

66 views
Skip to first unread message

Derek

unread,
Jun 23, 2016, 2:44:10 PM6/23/16
to django-users
Hi

I have a strange situation (with Django 1.6.11 and MySQL 5.5). Attempting to save a new entry (from the admin) with values that violate the duplication restriction imposed by the unique_together constraint - in other words a duplicate of a record already in the database - raises an IntegrityError (stack trace listed at the end).

The model looks like something like :

    name = CharField(
        max_length=100)
    title = ForeignKey(Title)
    city = CharField(
        max_length=50,
        default=CITY_DEFAULT),
    occupation = ForeignKey(Occupation)
    
    class Meta:
        ordering = ['name']
        unique_together = (('name', 'title', 'city', 'occupation', ),)

I have also tested this situation in the shell. A normal clean() will not raise any error, but a full_clean() will. Does that imply the code path being triggered by an attempted save from within the admin does not call full_clean() or attempt to trap an IntegrityError?

I should add that `city` field is not on the form to be filled in; and is added in the clean method.

What is the best / correct solution to this?

Thanks!
Derek


Traceback:
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  465.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  198.             return view(request, *args, **kwargs)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  25.                 return func(self, *args2, **kwargs2)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
  371.                 return func(*args, **kwargs)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
  1164.                 self.save_model(request, new_object, form, False)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in save_model
  893.         obj.save()
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/models/base.py" in save
  545.                        force_update=force_update, update_fields=update_fields)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  573.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
  654.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/models/base.py" in _do_insert
  687.                                using=using, raw=raw)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
  232.         return insert_query(self.model, objs, fields, **kwargs)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
  1514.     return query.get_compiler(using=using).execute_sql(return_id)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  903.             cursor.execute(sql, params)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
  69.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
  53.                 return self.cursor.execute(sql, params)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__
  99.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
  53.                 return self.cursor.execute(sql, params)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py" in execute
  124.             return self.cursor.execute(query, args)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/MySQLdb/cursors.py" in execute
  205.             self.errorhandler(self, exc, value)
File "/home/derek/.virtualenvs/reg/local/lib/python2.7/site-packages/MySQLdb/connections.py" in defaulterrorhandler
  36.     raise errorclass, errorvalue

Exception Type: IntegrityError at /admin/collective/staff/add/
Exception Value: (1062, "Duplicate entry 'Derek-4-london-7' for key 'name'")

Simon Charette

unread,
Jun 23, 2016, 3:26:33 PM6/23/16
to Django users
Hi Derek,

It's hard to tell without your exact model admin definitions but I suspect this
might be related to #25987[1] which is fixed in Django 1.10.

By the way you should at least upgrade to 1.8 as your Django version is
not supported anymore and you could be exposed to multiple security issues.

Cheers,
Simon

[1] https://code.djangoproject.com/ticket/25987

Derek

unread,
Jun 23, 2016, 3:33:39 PM6/23/16
to django-users
Thanks Simon

Those definitions are the closest I can get to the original in terms of type and relationship; what other info is needed?

That ticket refers to child models inline - not my situation.

Upgrading is on the cards - but still has to be budgeted for....

Derek

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/MsJy7f7JUvU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a589540b-c491-45c9-9d1c-ba47d7cef728%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages