[Django]-Django + PostgreSQL trigger causes "violates foreign key constraint"

4đź‘Ť

âś…

This problem is by a “mutating table”, when DJANGO emulate the ON_CASCADE, first delete the row on table “ProductPricing” and the insert on “OldProductPricing” is over a row in “Product” which will disappear. You have other options:

  1. Delete the trigger and manage the logic in the Product.save() and ProductPricing.save(), and change on_delete=CASCADE by on_delete=DO_NOTHING.

or

  1. A log table (“OldProductPricing”) should keep the data for ever, and show all the changes in the history from “Product”, thus you should delete the foreign key from “OldProductPricing” to “Product.

Leave a comment