Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Foreign key constraint without checking existing data in Postgres

Tags:

postgresql

ALTER TABLE <table name> WITH NOCHECK
    ADD CONSTRAINT attachments_user_id_fkey FOREIGN KEY (user_id)
    REFERENCES public.users (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;

The above query is throwing the following error ERROR: syntax error at or near "WITH NOCHECK"

like image 624
krish Avatar asked May 06 '26 21:05

krish


1 Answers

That appears to be SQL Server syntax. PostgreSQL doesn't support that WITH NOCHECK, I think you want:

ALTER TABLE <table name>
ADD CONSTRAINT attachments_user_id_fkey FOREIGN KEY (user_id)
REFERENCES public.users (id) NOT VALID

You'll have to check the documentation to see if there are equivalents for the rest of the options you're trying to use.

like image 199
mu is too short Avatar answered May 08 '26 11:05

mu is too short