How is it possible that when I try to add a unique constraint named "serveurs_domain" it says I can’t because the constraint already exists. BUT, when I try to drop the constraint "serveurs_abbr" it says it doesn’t exist.
I’m confused ???
- SQL :
ALTER TABLE serveurs
DROP CONSTRAINT serveurs_domain;
ERROR: constraint « serveurs_domain » of relation « serveurs » does not exist
ALTER TABLE serveurs
ADD CONSTRAINT serveurs_domain UNIQUE (domain);
ERROR: constraint « serveurs_domain » already exists
- PgSQL Version :
select version();
PostgreSQL 13.8 (Debian 13.8-0+deb11u1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2
Answers
I found the solution.
I should use
DROP INDEX serveurs_domain;
instead ofALTER TABLE ...
The error correctly translated would have shown you the issue:
From pg_class:
So you are trying to reuse an existing index(relation) name and that won’t work. FYI, it is a good habit to do:
In which case a
NOTICE
not aERROR
is thrown.