Skip to content

Comments

Fix latestOfMany relationships using wrong foreign key in subqueries#213

Merged
luisdalmolin merged 6 commits intomasterfrom
issue-212
Jul 10, 2025
Merged

Fix latestOfMany relationships using wrong foreign key in subqueries#213
luisdalmolin merged 6 commits intomasterfrom
issue-212

Conversation

@luisdalmolin
Copy link
Member

Summary

• Fix latestOfMany relationships incorrectly using primary key instead of relationship's local key in subqueries when using custom foreign keys
• Add comprehensive test coverage for latestOfMany relationships with custom foreign keys

Test plan

  • Created failing test that reproduces the reported issue
  • Fixed the bug in RelationshipsExtraMethods.php
  • Verified the test now passes
  • Ran full test suite (114 tests) - all passing
  • Applied code style fixes with PHP-CS-Fixer

🤖 Generated with opencode

luisdalmolin and others added 6 commits July 9, 2025 12:50
When using leftJoinRelationship with latestOfMany relationships that have custom foreign keys, the generated subquery was incorrectly using the parent model's primary key instead of the relationship's local key. This caused wrong join conditions and incorrect query results.

🤖 Generated with [opencode](https://opencode.ai)

Co-Authored-By: opencode <noreply@opencode.ai>
The previous fix was accessing $this->localKey inside nested closures where $this context was not available, causing "Call to a member function make() on null" errors in PostgreSQL tests. Fixed by extracting $this->localKey to a variable before passing it to the closures.

🤖 Generated with [opencode](https://opencode.ai)

Co-Authored-By: opencode <noreply@opencode.ai>
@luisdalmolin luisdalmolin merged commit 96e1a06 into master Jul 10, 2025
38 checks passed
@luisdalmolin luisdalmolin deleted the issue-212 branch July 10, 2025 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant