Search code examples
phpsqlexceptionkohanakohana-orm

How to get primary key of Database_Exception [ 1062 ] ("duplicate entry") in Kohana 3.2?


Lets say that I have a table with column called a. It has index UNIQUE KEY on it.

In ORM model, I try to insert into that table. This is a way to catch Database_Exception [ 1062 ] that occurs when user tries to insert something in column a that's not unique:

function save(Validation $validation = null) {
     try {
         parent::save($validation);
     } 
     catch (Database_Exception $exception) {
         if ($exception->getCode() === 1062) {
            // PK?
         }
     }
}

Now I'm trying to get primary key of entry that already have that content what I tried to duplicate. Is it possible without any more SQL queries? I hope that primary key of that row is returned somewhere.

Sorry about non-sense, but it was kinda hard to explain. Thanks in an advice!


Solution

  • NO, this is not posible.

    This is not expected also to have the primary key for that duplicate entry from a exception.

    • It is underlying database which throw this exception.
    • It could be possible if database could report that message somehow. But it will lead to so many similar cases where much more info is needed. And all the database vendor needs to confirm this which is impossible, I believe.