Search code examples
sqlpostgresqlconstraintsaltersql-drop

Error message when dropping constrain


Every time I want to drop a constraint from a column I get an error. I can't see the problem. I am using postgres.

So I have created a table with two columns:

CREATE TABLE TableA(
person_id INT PRIMARY KEY,
lastname CHAR(100)
)

I use the code

ALTER TABLE TableA DROP CONSTRAINT person_id

to DROP the constraint from person_id but then I get an error:

Error : ERROR:  constraint "person_id" of relation "tablea" does not exist

What's the problem?


Solution

  • Primary keys in PostgreSQL are by default called <table>_pkey, so you probably want something like this:

    ALTER TABLE TableA DROP CONSTRAINT TableA_pkey;
    

    You can check the names for example in psql using \d TableA.