Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

CodeIgniter 4: try/catch does not catch a failed insert in the model

ErrorDuplicate entry '...' for key

Short answer

CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask. With DBDebug off, the insert just returns false. Turn DBDebug on, catch DatabaseException, or read the error with $db->error().

  • Applies to CodeIgniter 4.5, PHP 8.3, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check the DBDebug value of the database group your model uses. It decides whether a failed query throws.

    grep -n "DBDebug" app/Config/Database.php
  2. 2Match what you see, then jump to its fix

    1. 1The catch block never runs and no exception appearsDBDebug is false, so nothing is thrownFix
    2. 2The exception is not thrown even though DBDebug is trueThe insert runs inside a transactionFix
    3. 3DBDebug is false on purposeYou want to handle the error without an exceptionFix

What does the error look like?

  • Duplicate entry '...' for key
  • DatabaseException

Cause 1 of 3

DBDebug is false, so nothing is thrown

1Check

DBDebug controls whether database errors throw exceptions. The default group ships with it set to true, so look for a false in your own config or in an environment override.

grep -rn "DBDebug" app/Config/

2Fix

Set DBDebug to true for the group, and catch the exception.

use CodeIgniter\Database\Exceptions\DatabaseException;

try {
    $this->insert($data);
} catch (DatabaseException $e) {
    log_message('error', $e->getMessage());
}

3Verify

Insert the same unique value twice; the second call lands in the catch block.

echo $e->getMessage(); // inside the catch block

Confirmed by codeigniter.com

Did this fix it?

Cause 2 of 3

The insert runs inside a transaction

1Check

Since 4.3.0, a failed query inside a transaction does not throw by default, even with DBDebug on.

grep -rn "transStart\|transBegin" app/

2Fix

Call transException(true) before transStart, and catch DatabaseException outside the block. The transaction is rolled back before it throws.

try {
    $this->db->transException(true)->transStart();
    $this->insert($data);
    $this->db->transComplete();
} catch (DatabaseException $e) {
    // already rolled back
}

3Verify

A duplicate insert now reaches the catch block and leaves no partial rows.

echo $e->getMessage(); // inside the catch block

Confirmed by codeigniter.com

Did this fix it?

Cause 3 of 3

You want to handle the error without an exception

1Check

With exceptions off, a failed query does not throw. The error stays on the connection, where the code and message can be read.

grep -rn "->error()" app/

2Fix

After the insert, read the code and message from the connection's error method. MySQL uses code 1062 for a duplicate entry.

$this->insert($data);
$error = $this->db->error(); // keys: code, message
if ($error['code'] === 1062) {
    // duplicate value
}

3Verify

A duplicate insert gives code 1062 in the array.

echo $e->getMessage(); // inside the catch block

Confirmed by codeigniter.com

Did this fix it?

How do I stop it coming back?

  • Decide per environment whether errors throw, and write it down in the database config.
  • Add a unique index and handle the duplicate case deliberately.
  • Log the exception message, not only a friendly page.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.