Testing a value against a constraint?

From: Benjamin Smith <bens(at)effortlessis(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Testing a value against a constraint?
Date: 2004-07-12 00:53:20
Message-ID: 200407111753.20904.bens@effortlessis.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm writing an intranet app in PHP, and having issues around constraints.
Specifically, the error messages coming back from PG aren't very user
friendly. I'm looking for a way to make this a bit more smooth to the end
user, and ensure that my conditional checks really do match the requirements
set forth in the database.

For example, given a check constraint called "check_productcode" is it
possible to test a value against that constraint alone, without attempting to
insert anything?

Something like

"select check constraint check_productcode('testvalue')"

where "testvalue" is the string you wish to check against constraint called
"check_productcode". It might return an error message or not.

Can this be done? How? I can't find this in the docs anywhere...

Right now, I have lots of code like this:

<?

Function CheckInvoice($invoice)
{
$productcodes=array('E1', 'E2', 'E3', 'F11');
if (!in_array($productcodes($invoice['productcode'], $productcodes))
return "Error: Product code is not valid!";
...
}

This is a tedious, duplicative, and error-prone way just to get some
plain-english error messages, since anytime a constraint needs to be changed,
it requires a code audit. Ugh.

Is there a better way?

-Ben

--
"I kept looking around for somebody to solve the problem.
Then I realized I am somebody"
-Anonymous

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jim Seymour 2004-07-12 00:54:34 Re: Do we need more emphasis on backup?
Previous Message Bruce Momjian 2004-07-12 00:19:22 Re: Do we need more emphasis on backup?