Setting constraint for two unique fields in PostgreSQL
constraints, postgresql
Solution
You can do what you are already thinking of: create a unique constraint on both fields. This way, a unique index will be created behind the scenes, and you will get the behavior you need. Plus, that information can be picked up by `information_schema` to do some metadata inferring if necessary on the fact that both need to be unique. I would recommend this option. You can also use triggers for this, but a unique constraint is way better for this specific requirement.
Problem
I'm new to postgres. I wonder, what is a PostgreSQL way to set a constraint for a couple of unique values (so that each pair would be unique). Should I create an `INDEX` for `bar` and `baz` fields? ``` CREATE UNIQUE INDEX foo ON table_name(bar, baz); ``` If not, what is a right way to do that? Thanks in advance.