Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is PostgreSQL saying a constraint doesn't exist when I try to drop it?

Tags:

postgresql

I'm working with PostgreSQL 9.1.

In psql I'm using this query to discover what constraints I have in a certain DB...

SELECT
  *
FROM
  information_schema.constraint_table_usage
WHERE
  constraint_table_usage.table_catalog = 'journal_app_development'
AND
  constraint_table_usage.constraint_name NOT LIKE '%_pkey';

Output...

        table_catalog      | table_schema | table_name |   constraint_catalog    | constraint_schema | constraint_name 
  -------------------------+--------------+------------+-------------------------+-------------------+-----------------
   journal_app_development | public       | users      | journal_app_development | public            | fk_entry_user
   journal_app_development | public       | users      | journal_app_development | public            | fk_user_task
  (2 rows)

But when I try to drop a constraint I get an error...

# ALTER TABLE users DROP CONSTRAINT "fk_entry_user";
ERROR:  constraint "fk_entry_user" of relation "users" does not exist

Any idea what I'm doing wrong?


Background info / motivation

The reason I'm doing this is that I want to write a script (specifically a Rake task) that will drop constraints in the DB that aren't primary keys.

like image 268
Ethan Avatar asked Sep 25 '12 21:09

Ethan


1 Answers

Try looking at the table:

information_schema.table_constraints 

where the constraint_type column <> 'PRIMARY KEY'. I believe that should give you the other side of the relationship.

I believe you are trying to drop the constraint from the referenced table, not the one that owns it.

like image 87
jcern Avatar answered Nov 16 '22 02:11

jcern