When trying to delete duplicates with:
DELETE FROM staff
WHERE staff_id NOT IN (SELECT MIN(staff_id) FROM staff GROUP BY store_id);
There is an error:
[23503] ERROR: update or delete on table "staff" violates foreign key constraint "payment_staff_id_fkey" on table "payment" Detail: Key (staff_id)=(3) is still referenced from table "payment".
Indeed, payment has FK constraint:
CREATE TABLE public.payment
(
payment_id integer NOT NULL DEFAULT nextval('payment_payment_id_seq'::regclass),
customer_id integer NOT NULL,
staff_id smallint NOT NULL,
rental_id integer NOT NULL,
amount numeric(5,2) NOT NULL,
payment_date timestamp without time zone NOT NULL,
CONSTRAINT payment_pkey PRIMARY KEY (payment_id),
CONSTRAINT payment_customer_id_fkey FOREIGN KEY (customer_id)
REFERENCES public.customer (customer_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT,
CONSTRAINT payment_rental_id_fkey FOREIGN KEY (rental_id)
REFERENCES public.rental (rental_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT payment_staff_id_fkey FOREIGN KEY (staff_id)
REFERENCES public.staff (staff_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT
)
WITH (
OIDS=FALSE
);
It is not possible to set payment.staff_id = null
because of NOT NULL
as suggested here.
What is the easiest way to solve this?