7

I want to delete a constraint only if it exists. But it's not working or I do something wrong.

Here is my query:

IF EXISTS (SELECT * FROM information_schema.table_constraints WHERE constraint_name='res_partner_bank_unique_number')
THEN ALTER TABLE res_partner_bank DROP CONSTRAINT res_partner_bank_unique_number;

It gives me this error:

ERROR: syntax error at or near "IF"

If anybody can help me please. Thanks.

Regards.

1 Answer 1

13

There is no IFin SQL (only in PL/pgSQL).

But drop constraint supports the IF NOT EXISTS option:

ALTER TABLE res_partner_bank 
  DROP CONSTRAINT IF EXISTS res_partner_bank_unique_number;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.