# How catch SQL Exception?

**URL:** <https://discourse.cakephp.org/t/how-catch-sql-exception/1697>\
**Category:** Need Help\
**Created:** [December 9, 2016, 12:59pm UTC](https://discourse.cakephp.org/t/how-catch-sql-exception/1697 "2016-12-09T12:59:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [December 9, 2016, 12:59pm UTC](https://discourse.cakephp.org/t/how-catch-sql-exception/1697/1 "2016-12-09T12:59:02Z")

</div>

Hi All,  
How Can I catch SQL error "Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry"  
in Cake style?  
For my app this error is a normal, but it gets me each time new record in log file.

Thank you.

---

<div class="post-metadata">

**Author:** ![reuben.helms](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/reuben.helms/32/172_2.png) [@reuben.helms](https://discourse.cakephp.org/u/reuben.helms)\
**Post date:** [December 11, 2016, 12:06pm UTC](https://discourse.cakephp.org/t/how-catch-sql-exception/1697/2 "2016-12-11T12:06:08Z")

</div>

You might need to show the Controller or Model code that is causing this error, and perhaps the table definition.

I’ve not actually used the new ORM yet, but missing a $model-\>create() or trying to set the value on a column with a unique or primary index when it already exists might trigger something like this. Or perhaps the table is missing an auto\_increment on the primary key.

---

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [December 11, 2016, 4:58pm UTC](https://discourse.cakephp.org/t/how-catch-sql-exception/1697/3 "2016-12-11T16:58:06Z")

</div>

Hi,  
As I told it’s normal behavior, it happens because partner’s postback sends data twice and transaction\_id is unique.  
I’d like catch this error and suppress without records in error log.

---

<div class="post-metadata">

**Author:** ![reuben.helms](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/reuben.helms/32/172_2.png) [@reuben.helms](https://discourse.cakephp.org/u/reuben.helms)\
**Post date:** [December 11, 2016, 9:18pm UTC](https://discourse.cakephp.org/t/how-catch-sql-exception/1697/4 "2016-12-11T21:18:22Z")

</div>

The difficult thing is that SQL typically doesn’t throw exceptions, and if  
there is one, it’s throw by cakephp and isn’t specific to a create issue.  
Even if you can catch the error, the ORM will always log any SQL error.

If it’s a known issue, it might be better to do a find on the unique  
transaction ID, and if the record already exists, drop the rest of the  
processing.

---

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [December 12, 2016, 12:04am UTC](https://discourse.cakephp.org/t/how-catch-sql-exception/1697/5 "2016-12-12T00:04:56Z")

</div>

Easiest way to set validation rule isUnique, and Cake does it, bu it is additional server load

---

<div class="post-metadata">

**Author:** ![mamchenkov](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mamchenkov/32/44_2.png) [@mamchenkov](https://discourse.cakephp.org/u/mamchenkov)\
**Post date:** [December 15, 2016, 9:37am UTC](https://discourse.cakephp.org/t/how-catch-sql-exception/1697/6 "2016-12-15T09:37:07Z")

</div>

You can catch the \PDOException. Here is an example code that does exactly that:

> <https://github.com/QoboLtd/cakephp-csv-migrations/blob/master/src/Controller/AppController.php#L104>

---

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [December 16, 2016, 4:03pm UTC](https://discourse.cakephp.org/t/how-catch-sql-exception/1697/7 "2016-12-16T16:03:22Z")

</div>

Спасибо за ваш ответ.

Thanks for answer, it’s interesting, but I though it’s possible with CakeException class.
