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.

pdoherty926
  • 9,895
  • 4
  • 37
  • 68
  • 1
    could this help? https://stackoverflow.com/questions/8016776/can-postgresql-have-a-uniqueness-constraint-on-array-elements – Cyzanfar Oct 25 '17 at 20:46
  • @Cyzanfar Thanks. Unfortunately, the accepted answer isn't applicable and the other solution doesn't work on text arrays. – pdoherty926 Oct 25 '17 at 22:02
  • Got you. will continue to look around see if I can find anything pertinent. – Cyzanfar Oct 25 '17 at 22:07

0 Answers0