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).
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'")