Opened 8 years ago

Closed 6 years ago

#16029 closed Bug (invalid)

IntegrityError at /admin/flatpages/flatpage/add/

Reported by: dfcode Owned by: nobody
Component: contrib.admin Version: 1.6
Severity: Normal Keywords: admin, auth, logging, router, sessions
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


In attempt to share users and sessions across multiple Django projects, I have created a database router for the session and auth models. This caused a hiccup for the admin site when I needed to generate a superuser in the remote database so I could login to /admin/. It seemed to work fine until I tried to make a change and the following error occured:

IntegrityError at /admin/flatpages/flatpage/add/
insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_fkey"
DETAIL: Key (user_id)=(2964) is not present in table "auth_user".

It seems that the logging system is not honoring my router for the 'auth' model.

Change History (5)

comment:1 Changed 8 years ago by Karen Tracey

Resolution: needsinfo
Status: newclosed

There's not enough information here to evaluate where the problem is, nor if what you are trying to do is even possible given the current constraints of multi-db. One of those constraints is no ForeignKeys crossing databases (see The error is getting reported for an admin model (the admin log entry model: that has a ForeignKey to the auth User model. Has your routing setup taken into account how to deal with non-auth models that may have ForeignKeys to auth-provided models such as User?

comment:2 Changed 6 years ago by lukazb@…

Resolution: needsinfo
Status: closednew
UI/UX: unset
Version: 1.31.6

I am experiencing similar error in the admin interface with Django 1.6.1. I have a custom user model and multiple database setup. It happens every time I try to perform a save action on any model. There are only two databases and no foreign-key relations crossing over.
Two unexpected factors:
1) missing key value is very high (148) and I currently have only 1 user on my development machine, so why 148? I might have deleted some accounts, but I doubt it was as many as 148...
2) relation target table is auth_user, while I do have a custom user model which is a separate table (user_client). Why does the admin enforce relation to the auth_user table (which is empty btw)?

comment:3 Changed 6 years ago by lukazb@…

the error persists with single database setup. it seems the custom user model is the culprit.

comment:4 Changed 6 years ago by Claude Paroz

It might be an issue with a incomplete migration to a custom user model. To debug that, we'd need the result of ./ sqlall admin on one side and the real structure/constraints of your database regarding the django_admin_log table.

comment:5 Changed 6 years ago by lukazb@…

Resolution: invalid
Status: newclosed

that's exactly reason. the custom user model had been improperly registered with the admin interface. my bad :)

Note: See TracTickets for help on using tickets.
Back to Top