I would like to ALTER my existing table by adding CONSTRAINT to specific column to respect some regex.
I try to normalize phone prefix number by executing:
ALTER TABLE users
ADD CONSTRAINT users_prefix_constraint
CHECK (users.phone_prefix ~ '^\+\d{1,3}$');
And it's working correctly.
But I would like to use it more generic and define DOMAIN which holds regex pattern, to use the same pattern in different places (i.e. in triggers). That's why I imagine to do something like:
CREATE DOMAIN ph_prefix AS TEXT CHECK (VALUE ~'^\+\d{1,3}$');
ADD CONSTRAINT users_prefix_constraint CHECK (users.phone_prefix :: ph_prefix);
Problem here is, that CHECK part expects some function returning boolean, and casting doesn't.
- Is there a way to do it?
- Is that a good way to define constraint? Maybe it's not preferred way.