Skip to content

Conversation

@michalsn
Copy link
Member

Description
This PR fixes a bug in Postgre and SQLite3 where composite indexes are not fully taken into account when using upsert().

Both handlers:

  • requires specifying all columns in the composite unique constraint
  • and cannot mix columns from multiple unique constraints

Fixes #9450

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@michalsn michalsn added 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 labels Feb 19, 2025
@michalsn michalsn requested review from kenjis, paulbalandan, samsonasik and sclubricants and removed request for sclubricants February 19, 2025 08:50
@sclubricants
Copy link
Member

Posgre has very different functioning then MySQL when it comes to upserts. Postgre offers much more features while mysql automatically applies any and all constraints. It was attempted apply the primary key if constraints were not set. Its really just a guess but it was done to try and provide the same functionality across all databases. For all the DBMS except mysql it is really recommended to use the onConstraint() method and explicitly define them. Perhaps the documentation should highlight this.

@michalsn
Copy link
Member Author

While I agree that onConstraint() should be used when dealing with multiple keys, in cases where we have one composite key, it should be automatically applied as a whole and not partially.

@michalsn michalsn merged commit 686e9ae into codeigniter4:develop Mar 10, 2025
50 checks passed
@michalsn
Copy link
Member Author

Thank you @paulbalandan and @samsonasik

@michalsn michalsn deleted the fix/upsert-composite-index branch April 16, 2025 06:49
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

Successfully merging this pull request may close these issues.

Bug: Issue with Composite Primary Keys in _upsertBatch for PostgreSQL – Only the First Index Field Is Considered

4 participants