Postgres ALTER TABLE ADD CONSTRAINT IF NOT EXISTS not working - postgresql

I need to add a Constraint if not exists and am hitting the following error. Note that a similar if not exists for a new Column, right above it, does work. There's some syntax error when adding a Constraint, am I missing something?
alter table requests_t
add constraint if not exists
valid_bias_check CHECK (bias_flag::text = ANY (ARRAY['Y'::character varying::text, 'N'::character varying::text]));
Error
ERROR: syntax error at or near "not"
LINE 2: add constraint if not exists

Since Postgres doesn't support this syntax with constraints (see a_horse_with_no_name's comment), I rewrote it as:
alter table requests_t
drop constraint if exists valid_bias_check;
alter table requests_t
add constraint
valid_bias_check CHECK (bias_flag::text = ANY (ARRAY['Y'::character varying::text, 'N'::character varying::text]));

Related

Cannot drop index because unique constraint requires it (postgres)

Running Postgres 9.6.
I'm trying to write database migration code to drop a redundant unique constraint and index. The problem is that in some installations these index and constraint exist, and in others they don't.
The recommended way to drop would be:
alter table <table_name> drop constraint <unique_name>
But that fails when the constraint does not exist, and there is no "if exists" clause for this. There is an "if exists" clause to the "drop index" command, but that initially fails as well:
db=> drop index if exists <unique_name>;
ERROR: cannot drop index <unique_name> because constraint <unique_name> on table <table_name> requires it
But wait, "drop index" has a "cascade" option to remove dependent objects, so we can use that!
Well, no. I get the same response:
db=> drop index if exists <unique_name> cascade;
ERROR: cannot drop index <unique_name> because constraint <unique_name> on table <table_name> requires it
Note: I've seen a lot of seemingly-related answers where "cascade" solved the problem for people, but these all mention foreign-key constraints, not unique constraints.
Note: This does not relate to the fact that cascade is not supported when trying to remove the index concurrently; when you add the "concurrently" keyword, you get a very explicit
ERROR: DROP INDEX CONCURRENTLY does not support CASCADE
Is this a known bug? Or am I missing something?
You can use a DO statement and trap the error:
DO
$$BEGIN
ALTER TABLE <table_name> DROP CONSTRAINT <unique_name>;
EXCEPTION
WHEN undefined_object
THEN NULL; -- ignore the error
END$$;

Facing error while Altering table in PostgreSQL

I was creating tables in PostgreSQL. Then I had to use an column as foreign key so I altered my table to define that column as foreign key.
But I got an error at "WITH",
ALTER TABLE Account WITH NOCHECK ADD CONSTRAINT FK_Account_AccountCPCMapping FOREIGN KEY(nAccountCPCMappingID)
REFERENCES AccountCPCMapping (nAccountCPCMappingID);
I am getting error like,
ERROR: syntax error at or near "WITH"
LINE 1: ALTER TABLE Account WITH NOCHECK ADD CONSTRAINT FK_Account...
Please suggest any corrections.
you try to use Microsoft sql server syntax https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql
while Postgres syntax https://www.postgresql.org/docs/current/static/sql-altertable.html is
...ADD table_constraint [ NOT VALID ]

Postgres drop exclusion constraint

I ran this DDL to add an exclusion constraint to a table:
ALTER TABLE recurring_charges
ADD EXCLUDE USING GIST (period WITH &&);
Now I want to remove the constraint - how do I do that? I tried some variations of ALTER TABLE ... DROP EXCLUDE and DROP CONSTRAINT but nothing seems to work.
Figured it out - looks like it generated a name for the constraint that I have to use.
ALTER TABLE recurring_charges
DROP CONSTRAINT recurring_charges_period_excl;
And now I've updated my original DDL to use the full ADD CONSTRAINT syntax so that I can name my constraint rather than relying on automatic naming behavior:
ALTER TABLE recurring_charges
ADD CONSTRAINT recurring_charges_period_excl EXCLUDE USING GIST (period WITH &&);
To add to the above answer, to find out the constraint name, you can find it in psql using:
\d+ tableName

constraint xxxxxxx is not a foreign key constraint

I'm importing millions of rows from different files.
One of those files throws an exception about a constraint:
PG::InternalError: ERROR: constraint 109531 is not a foreign key constraint (ActiveRecord::StatementInvalid)
How can I find the constrain 109531?
I can't find it on pg_constraint ...
Where I should look?
EDIT:
The error comes directly from PostgreSQL, Activerecord is passing it.
After adding a parent row manually using psql, now if I want to delete or update the row, I'm getting a new constrain error:
ERROR: constraint 109529 is not a foreign key constraint
If I try to find the constrain:
select * FROM pg_constraint;
I can see lots of constraints with similar numbers:
107878,109309,109521
Only with
TRUNCATE table CASCADE;
the row has been deleted
Well, after deleting ALL constraints and recreating them (with methods as there are more than 200 constraints), everything works perfect ...
I've used this link for the automated generated/removed constraints

Syntax error at or near "user" when adding Postgres constraint

I'm running Postgres 8.4.13, and trying to add a constraint to an existing table. According to the docs, this should be possible:
alter table indexed_friends add constraint no_duplicate_user_friends unique (user, friend);
Yet when I run this I get the following error:
ERROR: syntax error at or near "user"
I'm confused because I'm following an unique constraint example listed in the documentation almost exactly. I can provide the table schema, but since it's complaining about a syntax error, I'm not sure that's necessary.
Ahhh... The word user is a reserved word in Postgres.
Surrounding it in quotes:
alter table indexed_friends add constraint no_duplicate_user_friends unique ("user", friend);
worked.