Re: Own messages for constraints?

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Kacper Chrapa <k(dot)chrapa(at)wp(dot)pl>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Own messages for constraints?
Date: 2007-03-19 17:40:58
Message-ID: 1174326058.23455.363.camel@dogma.v10.wvs
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 2007-03-16 at 20:19 +0100, Kacper Chrapa wrote:
> Hi !
>
> Is it possible in postgres to define own message for some constraint?
>
> Example:
> If i create check constraint on zip_code column , i want to get
> a message: "Zip code is invalid.Please,input code in format: nn-nnn"
> and I want to send this message to the end user.
> It will be much better(in my case) than "violates constraint
> zip_code_check" :-) .
>
> I can make this validation in trigger (and send msg to application by
> notice or raise),but in this case i will duplicate validation
> rules (in trigger and in constraint).Effect: Lower performance(i think)
> and rules in two places...
>

There's no custom message for a CHECK constraint violation.

You can use an AFTER trigger instead of a CHECK constraint (but that may
have a performance impact - test for your application).

You can also give descriptive names to your CHECK constraint which may
help.

It's not a good idea to pass database errors directly back to the user
anyway.

Regards,
Jeff Davis

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hubert depesz lubaczewski 2007-03-19 18:08:41 Re: Own messages for constraints?
Previous Message Tom Lane 2007-03-19 17:30:20 Re: Planner tuning