1

I found this very similar question but all of the answer started with a 'select' statement. I want to check whether a string is contained in a constant array with about 30 other strings. I could write a long x == a OR x == b OR... statement but I thought there might be a cleaner way.

So this doesn't work as a constraint check: SELECT language = ANY ('{"en", "pt", "es", "fr"}'::text[])

Community
  • 1
  • 1
David Schumann
  • 11,499
  • 8
  • 67
  • 87

1 Answers1

1

Just removing the SELECT works:

CHECK(
    language = ANY ('{"en", "pt", "es", "fr"}'::text[])
)

But as a_horse_with_no_name pointed out:

Not using an array is even better, as this does not break the partitioning optimization.

CHECK(
    not( language in ('en', 'pt', 'es'))
)

Now SELECT * FROM myTable WHERE language='de'; will not even look at this table.

David Schumann
  • 11,499
  • 8
  • 67
  • 87