Open bruvellu opened 1 year ago
The possible solution is to drop the table and re-run the initial admin migration. Please refer to this ticket: https://code.djangoproject.com/ticket/23297
./manage.py dbshell
DROP TABLE django_admin_log;
\q
./manage.py sqlmigrate admin 0001
The output is:
BEGIN;
--
-- Create model LogEntry
--
CREATE TABLE "django_admin_log" ("id" integer NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "action_time" timestamp with time zone NOT NULL, "object_id" text NULL, "object_repr" varchar(200) NOT NULL, "action_flag" smallint NOT NULL CHECK ("action_flag" >= 0), "change_message" text NOT NULL, "content_type_id" integer NULL, "user_id" bigint NOT NULL);
ALTER TABLE "django_admin_log" ADD CONSTRAINT "django_admin_log_content_type_id_c4bce8eb_fk_django_co" FOREIGN KEY ("content_type_id") REFERENCES "django_content_type" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "django_admin_log" ADD CONSTRAINT "django_admin_log_user_id_c564eba6_fk_user_usercifonauta_id" FOREIGN KEY ("user_id") REFERENCES "user_usercifonauta" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "django_admin_log_content_type_id_c4bce8eb" ON "django_admin_log" ("content_type_id");
CREATE INDEX "django_admin_log_user_id_c564eba6" ON "django_admin_log" ("user_id");
COMMIT;
Apparently, this is not enough to create the table. An error relation "django_admin_log" does not exist
occurred. Running ./manage.py migrate
after that didn't work.
To fix, I had to copy the SQL commands above and paste them in the database prompt on ./manage.py dbshell
. The output was:
cebimar-# BEGIN;
--
-- Create model LogEntry
--
CREATE TABLE "django_admin_log" ("id" integer NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "action_time" timestamp with time zone NOT NULL, "object_id" text NULL, "object_repr" varchar(200) NOT NULL, "action_flag" smallint NOT NULL CHECK ("action_flag" >= 0), "change_message" text NOT NULL, "content_type_id" integer NULL, "user_id" bigint NOT NULL);
ALTER TABLE "django_admin_log" ADD CONSTRAINT "django_admin_log_content_type_id_c4bce8eb_fk_django_co" FOREIGN KEY ("content_type_id") REFERENCES "django_content_type" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "django_admin_log" ADD CONSTRAINT "django_admin_log_user_id_c564eba6_fk_user_usercifonauta_id" FOREIGN KEY ("user_id") REFERENCES "user_usercifonauta" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "django_admin_log_content_type_id_c4bce8eb" ON "django_admin_log" ("content_type_id");
CREATE INDEX "django_admin_log_user_id_c564eba6" ON "django_admin_log" ("user_id");
COMMIT;
ERROR: syntax error at or near "
INE 1:
^
CREATE TABLE
ALTER TABLE
ALTER TABLE
CREATE INDEX
CREATE INDEX
WARNING: there is no transaction in progress
COMMIT
This solved the issue, apparently.
Changing the default user model in the middle of a project is tricky. Many things can break in the database. And these need to be manually fixed or re-built to ensure a properly working project. The recommended way is to simply extend the default user model, using one-to-one relationships to add additional "profile" fields.
The particular error of this issue is a consequence of changing to a custom user model mid-project. The table
django_admin_log
still has a foreignkey toauth_user
:A possible solution for this issue is here: https://code.djangoproject.com/ticket/23297