Home > Mobile >  postgres check constraint on length of elements in text[]
postgres check constraint on length of elements in text[]

Time:11-29

Is there a possibility to add a check constraint to ensure the length of the elements in a text[] in postgres?

I want each element to have a length of 2 characters (country iso code).

Usecase: it's a filter. The entity saved in that row should only be active in the countries included in that array.

CodePudding user response:

It's possible, but ugly since you'd need a 2 = ALL (SELECT length(value) FROM unnest(arr)) subquery which you'd have to place in an immutable helper function like here.

Instead, why not just switch from an array of text to the appropriate character type: character(2)[]?

  • Related