Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: insertBatch throws a non catchable mysqli_sql_exception #9213

Open
mentorduncan opened this issue Oct 2, 2024 · 1 comment
Open

Bug: insertBatch throws a non catchable mysqli_sql_exception #9213

mentorduncan opened this issue Oct 2, 2024 · 1 comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them database Issues or pull requests that affect the database layer

Comments

@mentorduncan
Copy link

PHP Version

8.3

CodeIgniter4 Version

4.5.5

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

MariaDB 11.4.2

What happened?

ERROR - 2024-10-02 16:17:07 --> mysqli_sql_exception: Duplicate entry '620-43' for key 'fk_UNIQUE'

Steps to Reproduce

Create a pivot table, where the combination of the two fk columns is unique.

Feed it with some combinations of fks
And do an insertBatch where there is a collision.

Despite logging the error above, the script will run until the end.
And it was not possible to capture the error even with:
catch (DatabaseException $e)
catch (\mysqli_sql_exception $e)
or
catch (\Exception $e)

Expected Output

a more user-friendly
or catchable error for me to deal with.

Anything else?

No response

@mentorduncan mentorduncan added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 2, 2024
@kenjis kenjis added the database Issues or pull requests that affect the database layer label Oct 3, 2024
@ping-yee
Copy link
Contributor

ping-yee commented Nov 13, 2024

Cannot reproduce this error.

My steps to reproduce:

CREATE TABLE pivot_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    foreign_key_1 INT NOT NULL,
    foreign_key_2 INT NOT NULL,
    UNIQUE KEY fk_UNIQUE (foreign_key_1, foreign_key_2)
);
<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        $db = \Config\Database::connect();
        $builder = $db->table('pivot_table');

        $data = [
            ['foreign_key_1' => 5, 'foreign_key_2' => 6],
            ['foreign_key_1' => 5, 'foreign_key_2' => 6],
        ];
        
        $builder->insertBatch($data);
        echo "Data inserted successfully.";
    }
}

And it'll throw the DatabaseException.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them database Issues or pull requests that affect the database layer
Projects
None yet
Development

No branches or pull requests

3 participants