سؤال

I have a table for users that includes two columns for contact information. A user can provide a phone number, an email, or both, but not neither.

I have the table set up with the following constraints

create table application_user(
    -- other columns, primary key, etc.
    phone text,
    email text,
    constraint valid_email check (((email IS NULL) AND (phone IS NOT NULL)) OR (email ~ '(?:[a-z0-9!#$%&''*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])'::text)),
    constraint valid_phone check (((phone IS NULL) AND (email IS NOT NULL)) OR (phone ~ '\d{10}'::text))
);

The idea is to make sure the email is valid if present and the phone is valid if present. However, when I run the following insert statement:

INSERT INTO application_user (email, phone) VALUES (null, null);

The insert still completes. What am I doing wrong here? I'm using PostgreSQL 12 if that matters.

هل كانت مفيدة؟

المحلول

As the check constraint with the regex will only validate non-null values, I would split this up in three different check constraints:

create table application_user(
    -- other columns, primary key, etc.
    phone text,
    email text,
    constraint exactly_one check (num_nonnulls(phone, email)=1 ),
    constraint valid_email check (email ~ '(?:[a-z0-9!#$%&''*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])'::text),
    constraint valid_phone check (phone ~ '\d{10}'::text)
);

check (num_nonnulls(phone, email)=1) only validates that either one of the column is specified. The other two only check the format of the respective column. This - I think - makes things a bit easier to understand.

نصائح أخرى

Checking whether NULL matches the pattern was not returning false as I expected, it was itself returning NULL, thus not violating the constraint. I was able to fix it by adding another NULL check before the pattern match:

create table application_user(
    -- other columns, primary key, etc.
    phone text,
    email text,
    constraint valid_email check (((email IS NULL) AND (phone IS NOT NULL)) OR (email IS NOT NULL AND email ~ '(?:[a-z0-9!#$%&''*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])'::text)),
    constraint valid_phone check (((phone IS NULL) AND (email IS NOT NULL)) OR (phone IS NOT NULL AND phone ~ '\d{10}'::text))
);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى dba.stackexchange
scroll top