1

I have created a constraint this way:

ALTER TABLE  varaus  ADD FOREIGN KEY ( varaus_id ) REFERENCES  kayttaja  ( id );

But when trying to drop it:

ALTER TABLE varaus DROP CONSTRAINT varaus_id;

I get the error:

ERROR:  constraint "varaus_id" of relation "varaus" does not exist

I have also tried:

ALTER TABLE varaus DROP CONSTRAINT varaus_fkey;
ALTER TABLE varaus DROP CONSTRAINT id;
Sampo Kaarenmaa
  • 127
  • 2
  • 13

2 Answers2

2

You have to find the name of constraint first using below query -

select constraint_name
from information_schema.table_constraints
where table_schema = 'your_schema_name'
and table_name='varaus'
and constraint_name like 'fk_%'

Then use those names in below query -

ALTER TABLE varaus DROP CONSTRAINT constraint_name
Ankit Bajpai
  • 13,128
  • 4
  • 25
  • 40
0

Good example of why you should always name your constraints instead letting the DBMS generate a name. So instead of the initial creation of the FK use (for example)

alter table  varaus add  
      constraint var2kay_fk 
      foreign key ( varaus_id ) 
      references  kayttaja  ( id );
Belayer
  • 13,578
  • 2
  • 11
  • 22