I have a problem with my delete statement.
I have two tables:
table vehicule_loan(
vehicule TEXT NOT NULL UNIQUE,
);
table vehicule_uid (
id UUID NOT NULL DEFAULT uuid_generate_v4(),
vehicule TEXT NOT NULL REFERENCES vehicule_loan(vehicule) ON DELETE NO ACTION
);
When I delete a vehicule from the table vehicule_loan I want that referencing rows in the table vehicule_uid are kept.
But when I try to delete one I get this error:
ERROR: update or delete on table "vehicule_loan" violates foreign key constraint "vehicule_uid_vehicule_fkey" on table "vehicule_uid"
I think I understand the error:
After I delete a vehicule from the table vehicule_loan, the vehicule in vehicule_uid would point to nothing.
But is there a way to keep the rows in vehicule_uid ?
You should allow
NULLvalues in the foreign key attribute and define the foreign key constraint asON DELETE SET NULL.I quote chapter 5.3. Constraints from the PostgreSQL manual:
Could look like this:
With this setting, when you delete a row in
vehicule_loanall referencing rows invehicule_uidremain in database.