krish
krish

Reputation: 41

Adding Foreign key constraint without checking existing data in Postgres

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"

Upvotes: 3

Views: 5274

Answers (1)

mu is too short
mu is too short

Reputation: 434935

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.

Upvotes: 5

Related Questions