8

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.

Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81

4 Answers4

19

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.

Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
0

It seems you logged in with another user and changed some models. So you need to log out from the admin panel and log in again!

This solution worked fine for me.

Milad shiri
  • 812
  • 7
  • 5
0

You can comment out the models that you have updated. And run migrations using the following command:

python manage.py makemigrations app.

Now uncomment the models and again run same makemigrations command. All the details will be removed from the admin panel as well.

Vivian De Smedt
  • 1,019
  • 2
  • 16
  • 26
-1

Drop the database and again create it(Postgresql), It will solve the problem.