getting details about integrity constraint violation

Lists: pgsql-sql
From: Markus Bertheau <twanger(at)bluetwanger(dot)de>
To: "'PostgreSQL'" <pgsql-sql(at)postgresql(dot)org>
Subject: getting details about integrity constraint violation
Date: 2005-06-02 15:25:16
Message-ID: 1117725916.7345.2.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Hi,

how would I find out details about for example what constraint was
violated by an insert statement? The SQL state tells me, that a unique
constraint was violated, but it doesn't say which one. I cannot sensibly
react to such errors if I don't know what exactly happened.

I'd like to avoid parsing the text error message because it can be
different depending on the LC_MESSAGES the server / libpq runs with.

Markus

--
Markus Bertheau <twanger(at)bluetwanger(dot)de>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Markus Bertheau <twanger(at)bluetwanger(dot)de>
Cc: "'PostgreSQL'" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: getting details about integrity constraint violation
Date: 2005-06-02 16:32:09
Message-ID: 14879.1117729929@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Markus Bertheau <twanger(at)bluetwanger(dot)de> writes:
> how would I find out details about for example what constraint was
> violated by an insert statement?

You can't, at the moment, except by parsing the text message.

The "error fields" facility in the FE/BE protocol could be extended
in that direction, and I think there's already been some discussion
about it; but no one has stepped up with a concrete proposal, much
less volunteered to do the work ...

regards, tom lane


From: PFC <lists(at)boutiquenumerique(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Markus Bertheau" <twanger(at)bluetwanger(dot)de>
Cc: 'PostgreSQL' <pgsql-sql(at)postgresql(dot)org>
Subject: Re: getting details about integrity constraint violation
Date: 2005-06-03 18:42:34
Message-ID: op.srs4w8kzth1vuj@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql


> The "error fields" facility in the FE/BE protocol could be extended
> in that direction, and I think there's already been some discussion
> about it; but no one has stepped up with a concrete proposal, much
> less volunteered to do the work ...

Um, if changing the protocol is a bother, you could also add parseable
infos to the error messages...

instead of :
"ERROR: duplicate key violates unique constraint "testinteg_one_key""

it would say
"ERROR: duplicate key violates unique constraint "testinteg_one_key"
[code:"XXXX" error:"integrity" type:"unique" column:"something"
constraint:"testinteg_one_key"]"

Which could be hackfully added by a "parseable" locale (but with a more
restrained form...)
SET lc_messages TO parseable

>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>


From: "Greg Sabino Mullane" <greg(at)turnstep(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: twanger(at)bluetwanger(dot)de
Subject: Re: getting details about integrity constraint violation
Date: 2005-06-14 03:39:47
Message-ID: 873b36387522e9a014ec0c75cbb63338@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Markus Bertheau asked:
> how would I find out details about for example what constraint was
> violated by an insert statement? The SQL state tells me, that a unique
> constraint was violated, but it doesn't say which one.

Simply name the table constraints yourself with a descriptive name, so you
always know exactly what is going on:

greg=# create table unitest(a int, b text);
CREATE TABLE
greg=# alter table unitest add constraint "unitest_column_a_is_not_unique" unique(a);
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "unitest_column_a_is_not_unique" for table "unitest"
greg=# insert into unitest (a) values (1);
INSERT 0 1
greg=# insert into unitest (a) values (1);
ERROR: duplicate key violates unique constraint "unitest_column_a_is_not_unique"

- --
Greg Sabino Mullane greg(at)turnstep(dot)com
PGP Key: 0x14964AC8 200506121520
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

iD8DBQFCrIsevJuQZxSWSsgRAh+gAJ94AsB7rZzpxT7pogC1tgbPaQJzJQCg5YkC
E9dXkQk4qP8r8zjCEucxpt0=
=NDgJ
-----END PGP SIGNATURE-----


From: Markus Bertheau <twanger(at)bluetwanger(dot)de>
To: Greg Sabino Mullane <greg(at)turnstep(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: getting details about integrity constraint violation
Date: 2005-06-14 05:46:49
Message-ID: 1118728010.7429.0.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Dnia 14-06-2005, wto o godzinie 03:39 +0000, Greg Sabino Mullane
napisaƂ(a):

> Simply name the table constraints yourself with a descriptive name, so you
> always know exactly what is going on:

And then I keep a list of all the constraint names and scan the error
message for it?

Markus
--
Markus Bertheau <twanger(at)bluetwanger(dot)de>


From: "Greg Sabino Mullane" <greg(at)turnstep(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: twanger(at)bluetwanger(dot)de
Subject: Re: getting details about integrity constraint violation
Date: 2005-06-15 02:06:20
Message-ID: 93b83a5f4f948719c40b2ecac346c692@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>> Simply name the table constraints yourself with a descriptive name, so you
>> always know exactly what is going on:

> And then I keep a list of all the constraint names and scan the error
> message for it?

Don't keep a list: just come up with a standard naming scheme, such as:

"tablename|colname|is_not_unique"

which should be human and machine parseable (perl example):

if ($error =~ m#^(.+)\|(.+)\|is_not_unique$#o) {
die qq{Whoops : looks like column "$2" of table "$1" needs to be unique\n};
}

- --
Greg Sabino Mullane greg(at)turnstep(dot)com
PGP Key: 0x14964AC8 200506142204
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

iD8DBQFCr4zivJuQZxSWSsgRAgGPAJ0awkoBmus6z1cLBRpsR5xmQPTfiACgpJxG
Ld90hEGDPrebBE3JGGL11L4=
=smQJ
-----END PGP SIGNATURE-----