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: Issue with Composite Primary Keys in _upsertBatch for PostgreSQL – Only the First Index Field Is Considered #9450

Open
campio97 opened this issue Feb 12, 2025 · 0 comments · May be fixed by #9454
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@campio97
Copy link

PHP Version

8.4

CodeIgniter4 Version

4.6.0

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

apache

Database

PostgreSQL 17.2

What happened?

Hello,

I'm reporting a potential bug in CodeIgniter 4 related to the _upsertBatch function when used with PostgreSQL. It appears that the function only considers the first field of an index for conflict resolution, which causes issues when dealing with composite primary keys or tables with multiple unique indexes.

Details:

In the _upsertBatch function, the following code snippet suggests that only one index (and, in practice, only the first field of that index) is used:

foreach (array_map(static fn($index) => $index->fields, $allIndexes) as $index) {
    $constraints[] = current($index);
    // only one index can be used?
    break;
}

Because of this, if a table has a composite primary key (or multiple unique constraints), the generated SQL will include only the first field from the index in the ON CONFLICT clause. This behavior may lead to unexpected failures or incorrect updates during upsert operations.

Steps to Reproduce

Create a PostgreSQL table with a composite primary key or with multiple unique indexes.
Use the upsertBatch method to insert/update data in this table.
Observe that the conflict resolution logic only uses the first field of the composite key, which can lead to errors or improper upsert behavior.

Expected Output

All relevant fields of the composite key (or the chosen unique index) should be used in the ON CONFLICT clause to correctly identify conflicts and perform the update action.

Anything else?

This issue might be related to the comment // only one index can be used? present in the code. It suggests that the current implementation might be a temporary workaround or an oversight that did not fully account for composite keys.

@campio97 campio97 added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 12, 2025
@campio97 campio97 changed the title Bug: upsertBatch get multiple index from table Bug: Issue with Composite Primary Keys in _upsertBatch for PostgreSQL – Only the First Index Field Is Considered Feb 12, 2025
@michalsn michalsn linked a pull request Feb 18, 2025 that will close this issue
5 tasks
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant