Reputation: 2343
I'm creating something that needs to collect all types of data on your database, including the types of constraints used by each column. Unfortunately, it appears there is two ways to add constraints to a column (or maybe even more that I'm unaware of), you can either add a UNIQUE CONSTRAINT, or you can add an INDEX with UNIQUE on it.
I have this query:
select * from information_schema.table_constraints WHERE constraint_type='UNIQUE';
But the problem is that it does not detect UNIQUE constraints that are applied to an INDEX, it only detects UNIQUE CONSTRAINTS on their own, for example, it cannot find this:
Indexes:
"index_videos_on_slug" UNIQUE, btree (slug)
Does anyone know of a way I can get this information? Thanks.
Upvotes: 0
Views: 476
Reputation: 4513
pg_catalog
schemainformation_schema
is built on top of this (as a set of views)pg_index.indisunique
(in the pg_catalog schema) is what you want.
BTW: if you want to promote an existing index to a constraint, you can use the alter ... using ... syntax:
ALTER TABLE ... ADD constraint ... USING your_index_name;
Upvotes: 1