[odb-users] Get real error reason from object_already_persistent
Boris Kolpackov
boris at codesynthesis.com
Mon Aug 11 05:56:13 EDT 2014
Hi Mikhail,
Tomilov.Mikhail <Mikhail.Tomilov at infotecs.ru> writes:
> I need to insert a row into users table, which has unique constraint
> on "login" field and some FK. If I insert row with duplicate login
> field I get object_already_persistent exception. AFAIK
> object_already_persistent exception is thrown when any postgresql
> constraint fails. It could be PK constraint, unique constraint, FK
> constraint. Can I determine real reason for my case to pass correct
> error code to client?
>
> It would be natural to get database error info from
> odb::object_already_persistent exception.
The issue is described in Section 19.5.3, "Unique Constraint Violations"
in the ODB manual:
"Due to the granularity of the PostgreSQL error codes, it is impossible
to distinguish between the duplicate primary key and other unique
constraint violations. As a result, when making an object persistent,
the PostgreSQL ODB runtime will translate all unique constraint violation
errors to the object_already_persistent exception."
There is also an informative thread that goes into more details and
discusses possible solutions:
http://www.codesynthesis.com/pipermail/odb-users/2012-September/000785.html
On the second thought, the _pkey suffix idea doesn't sound bad. It
is rarely that anyone assigns an explicit constraint name to the
primary key.
Would you be willing to test the fix?
Boris
More information about the odb-users
mailing list