fix(database): qualify orderBy fields against the base table - #2306
Open
radoslav-grencik wants to merge 1 commit into
Open
radoslav-grencik wants to merge 1 commit into
radoslav-grencik wants to merge 1 commit into
Conversation
Benchmark ResultsComparison of Open to see the benchmark resultsNo benchmark changes above ±5%. Generated by phpbench against commit 47ff16f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SelectQueryBuilder::orderBy('name')renderedORDER BY `name`— an unqualified column name. As soon as the query joins another table that shares the column (e.g.authors JOIN publisherswhere both havename), SQLite raises:SQLite resolves an ambiguous bare column by the first table in the
FROMclause, which makes ordering dependent on join order rather than the base model's column — a latent correctness bug even where it does not error.Change
packages/database/src/Builder/QueryBuilders/SelectQueryBuilder.php—orderBy()now qualifies bare fields with the base model's table name, leaving dotted paths untouched:orderBy('name')→ORDER BY `authors`.`name`— resolved against the base table.orderBy('publishers.name')→ORDER BY `publishers`.`name`— a dotted path is a relation column, passed through unchanged so joined-table ordering keeps working.orderByRaw()shorthand and raw fragments are untouched.Why qualify at the builder level
OrderByStatementcannot distinguish "bare base column" from "already-qualified column" — it renders a quoted identifier either way, and quoting a dotted string twice (`authors.name`) would break every call site. The base-table qualification belongs where the model context is known:SelectQueryBuilder.Tests
tests/Integration/Database/Builder/SelectQueryBuilderTest.php:order_by_sql_generation— updated to the new qualified SQL (ORDER BY `books`.`title`) forASCandDESC; dotted-field generation asserted unchanged.order_by_qualifies_bare_field_with_base_table_and_leaves_dotted_fields_alone— pinsauthors.namevspublishers.namecompilation side by side.order_by_with_joined_table_uses_base_table_column_for_ambiguous_bare_field— regression:authors JOIN publishers(both with anamecolumn),orderBy('name')now returnsAlpha Author, Beta Authorinstead of raising the ambiguous-column error.