Skip to content

Conversation

@luisdalmolin
Copy link
Member

Summary

  • Fix critical bug in generateAliasForRelationship() where using time() for alias generation caused duplicate aliases when called multiple times within the same second
  • Replace time() with uniqid('', true) to ensure truly unique aliases are generated consistently
  • Add comprehensive test suite to verify the fix and prevent regressions

Technical Details

Problem

The generateAliasForRelationship method in JoinsHelper.php was using time() to generate unique aliases:

return md5($relationName.time());

Since most code executions complete within the same second, multiple calls to this method would produce identical aliases, causing later joins to overwrite earlier ones. This resulted in:

  • Silent query failures where only the last join would be applied
  • Inconsistent behavior depending on execution timing
  • Unreliable results when joining the same relationship multiple times

Solution

Replaced time() with uniqid('', true) which:

  • Uses current timestamp in microseconds (not just seconds)
  • Adds additional entropy from the system
  • Guarantees uniqueness even with rapid successive calls
  • Maintains the same MD5 hashing approach for consistency

Changes Made

  1. Core Fix: Updated src/JoinsHelper.php lines 157-158 and 162

    • md5($relationName.'table1'.time())md5($relationName.'table1'.uniqid('', true))
    • md5($relationName.'table2'.time())md5($relationName.'table2'.uniqid('', true))
    • md5($relationName.time())md5($relationName.uniqid('', true))
  2. Test Coverage: Added tests/DuplicateAliasGenerationTest.php with tests for:

    • Basic relationship alias uniqueness
    • BelongsToMany relationship alias uniqueness
    • Real-world query scenarios with multiple joins

Test plan

  • All existing tests pass (117/117)
  • New tests demonstrate the issue was fixed (3/3 passing)
  • Code style compliance maintained
  • No performance regressions
  • Verified fix works for HasMany, BelongsToMany, and HasManyThrough relationships

🤖 Generated with opencode

luisdalmolin and others added 2 commits July 10, 2025 12:47
…e times

Replace time() with uniqid() in generateAliasForRelationship() to ensure unique aliases are generated even when called multiple times within the same second. This prevents later joins from overwriting earlier ones due to identical alias names.

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

Co-Authored-By: opencode <[email protected]>
Update alias count check to handle both double quotes (PostgreSQL/SQLite) and backticks (MySQL) when counting table aliases in generated SQL.

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

Co-Authored-By: opencode <[email protected]>
@luisdalmolin luisdalmolin merged commit 72cff1e into master Jul 10, 2025
38 checks passed
@luisdalmolin luisdalmolin deleted the issue-211 branch July 10, 2025 16:55
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.

2 participants