When saving a new instance of a model in the admin, I get the following IntegrityError in a traceback:
Traceback:
File "/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
541. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
244. return view(request, *args, **kwargs)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
1435. return self.changeform_view(request, None, form_url, extra_context)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/utils/decorators.py" in inner
184. return func(*args, **kwargs)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/transaction.py" in __exit__
223. connection.commit()
File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in commit
242. self._commit()
File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in _commit
211. return self.connection.commit()
File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/utils.py" in __exit__
95. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/users/dan/project/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in _commit
211. return self.connection.commit()
Exception Type: IntegrityError at /admin/app/invitation/add/
Exception Value: insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_52fdd58701c5f563_fk_auth_user_id"
DETAIL: Key (user_id)=(3) is not present in table "auth_user".
The exact same code works perfectly on another machine.
It looks like my Django user table somehow got stale and contained an outdated primary key. Unfortunately, running ./manage.py migrate
didn't seem to fix anything, but with a little help from an old Django trac ticket, I was able to figure out how to easily fix this (warning, you will lose your admin logs).
./manage.py migrate admin 0001
echo "DROP TABLE django_admin_log;" | ./manage.py dbshell
./manage.py sqlmigrate admin 0001 | python manage.py dbshell
./manage.py migrate admin
I'm sure I could run an update in PostgreSQL to the same effect, but I didn't have any data I was worried about keeping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With