samedi 27 juin 2015

Django Error: Key is not present in table "auth_user"

I created a custom user table in Django and it was working fine until I tried to save a new user to the database. I get the error:

insert or update on table "customer_email" violates foreign key constraint "customer_email_user_id_8eeb3a31aa9371c_fk_auth_user_id"
DETAIL:  Key (user_id)=(7) is not present in table "auth_user".

After a bit of looking around, I found this link which says I should drop the table django_admin_log and re-sync the database. I did this but the error persists. What I think is that the after re-syncing, the table django_admin_log is not being created. I tried to view the table using psql but it says no such table exists.

What could be wrong here? How do I fix this.

Edit When I run syncdb or sqlmigrate, it shows the following in console, but I don't think the table is being created or changes are actually coming into effect.

BEGIN;
CREATE TABLE "django_admin_log" ("id" serial NOT NULL PRIMARY KEY, "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" integer NOT NULL);
ALTER TABLE "django_admin_log" ADD CONSTRAINT "djan_content_type_id_697914295151027a_fk_django_content_type_id" 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_52fdd58701c5f563_fk_userapp_user_id" FOREIGN KEY ("user_id") REFERENCES "userapp_user" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "django_admin_log_417f1b1c" ON "django_admin_log" ("content_type_id");
CREATE INDEX "django_admin_log_e8701ad4" ON "django_admin_log" ("user_id");

COMMIT;

Edit 2: I got the table to be created but the error still persists.

Edit 3: After further looking, I realised that the key is not in the table "auth_user" but in the table 'users', which is a custom model. So how do I fix this? I can set a meta class and set a db_table variable but that will mess up even more things..

Aucun commentaire:

Enregistrer un commentaire