0

Is it possible to add a uniqueness constraint to a Postgres text array? To clarify, I'd like to prevent duplicate instances of the same string in the same array.

I've tried a number of approaches without succes. Here's my most recent attempt:

alter table districts add constraint zip_idx exclude using gin (zip_codes with <@);
# access method "gin" does not support exclusion constraints

I've found this solution, which involves introducing a custom function, but I feel like there must be a way to handle this using one of the Gin operators or a contrib module.

Here's a baseline sqlfiddle.

3
  • 1
    could this help? stackoverflow.com/questions/8016776/… Commented Oct 25, 2017 at 20:46
  • @Cyzanfar Thanks. Unfortunately, the accepted answer isn't applicable and the other solution doesn't work on text arrays. Commented Oct 25, 2017 at 22:02
  • Got you. will continue to look around see if I can find anything pertinent. Commented Oct 25, 2017 at 22:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.