Opened 9 months ago

Closed 9 months ago

#23297 closed Uncategorized (worksforme)

Integrity errors in Django admin with custom User model

Reported by: dustinfarris Owned by: nobody
Component: contrib.auth Version: 1.7-rc-2
Severity: Release blocker Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by dustinfarris)

I use a custom User model: accounts.User

When trying to save anything in the Django admin, I get:

IntegrityError: insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_41e5d33e37dd3226_fk_auth_user_id"
DETAIL:  Key (user_id)=(4) is not present in table "auth_user".
Stacktrace (most recent call last):

  File "django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "django/contrib/admin/options.py", line 546, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "django/utils/decorators.py", line 105, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "django/views/decorators/cache.py", line 52, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "django/contrib/admin/sites.py", line 204, in inner
    return view(request, *args, **kwargs)
  File "django/contrib/admin/options.py", line 1416, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
  File "django/utils/decorators.py", line 29, in _wrapper
    return bound_func(*args, **kwargs)
  File "django/utils/decorators.py", line 105, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "django/utils/decorators.py", line 25, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "django/db/transaction.py", line 394, in inner
    return func(*args, **kwargs)
  File "django/db/transaction.py", line 339, in __exit__
    connection.commit()
  File "django/db/backends/__init__.py", line 176, in commit
    self._commit()
  File "django/db/backends/__init__.py", line 145, in _commit
    return self.connection.commit()
  File "django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "django/utils/six.py", line 549, in reraise
    raise value.with_traceback(tb)
  File "django/db/backends/__init__.py", line 145, in _commit
    return self.connection.commit()

Change History (3)

comment:1 Changed 9 months ago by dustinfarris

  • Component changed from contrib.sites to contrib.auth
  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Integrity error saving Site with custom User model to Integrity errors in Django admin with custom User model

comment:2 Changed 9 months ago by dustinfarris

Ack, classic case of me changing AUTH_USER_MODEL later in the project.

If anyone else runs into this, you need to re-create your django_admin_log table because it has a foreign key to the original user model:

./manage.py dbshell

DROP TABLE django_admin_log;

and then:

./manage.py sqlmigrate admin 0001 | ./manage.py dbshell

comment:3 Changed 9 months ago by dustinfarris

  • Resolution set to worksforme
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top