Skip to content

Conversation

@luisdalmolin
Copy link
Member

@luisdalmolin luisdalmolin commented Nov 12, 2025

Closes #223

This pull request introduces improvements to how table aliases are handled and preserved during join operations in Eloquent queries, specifically when using the joinRelationship method. The changes ensure that aliases defined in the main table's from clause are correctly maintained and referenced throughout join and nested join operations, preventing query errors and improving reliability. Additionally, targeted cache clearing for model aliases has been implemented to avoid unintended side effects.

Alias handling and preservation:

  • The joinRelationship method now detects if the main table has an alias in its from clause and registers this alias for use in joins, ensuring that all generated SQL references the correct alias.
  • When the model associated with a query is cloned, the original from clause (including any alias) is preserved and restored after the clone, maintaining the correct table alias throughout query manipulation. [1] [2]

Cache management:

  • After performing a join with an alias, only the related model's alias is cleared from the cache, rather than clearing the entire cache, which prevents loss of unrelated alias information.
  • Removed the previous blanket cache clearing after marking a relationship as joined, ensuring more precise cache management.

Testing:

  • Added multiple tests to verify that table aliases are correctly preserved and referenced in various scenarios, including basic joins, nested relationships, and queries with selected columns and where clauses.

@luisdalmolin luisdalmolin self-assigned this Nov 12, 2025
@luisdalmolin luisdalmolin merged commit ccda351 into master Nov 13, 2025
63 of 74 checks passed
@luisdalmolin luisdalmolin deleted the issue-223 branch November 13, 2025 14:57
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.

Aliases cannot be used on the main model

2 participants