Is there a way I can remove a constraint based on column names?
I have postgres 8.4 and when I upgrade my project the upgrade fails because a constraint was named something different in a different version.
Basically, I need to remove a constraint if it exists or I can just remove the constraint using the column names.
The name of the constraint is the only thing that has changed. Any idea if that’s possible?
In this case, I need to remove “patron_username_key”
discovery=# \d patron
Table "public.patron"
Column | Type | Modifiers
--------------------------+-----------------------------+-----------
patron_id | integer | not null
create_date | timestamp without time zone | not null
row_version | integer | not null
display_name | character varying(255) | not null
username | character varying(255) | not null
authentication_server_id | integer |
Indexes:
"patron_pkey" PRIMARY KEY, btree (patron_id)
"patron_username_key" UNIQUE, btree (username, authentication_server_id)
Assuming that unique index is the result of adding a unique constraint, you can use the following SQL statement to remove that constraint:
I’m not sure if this will work if you have “only” added a unique index (instead of a unique constraint).
If you need to do that for more than 2 columns you also need to adjust the
having count(*) = 2part to match the number of columns in thecolumn_name in ..condition.(As you did not specify your PostgreSQL version I’m assuming the current version)