Fix ambiguous ORDER BY clauses by fully qualifying column names #452
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.
This pull request addresses potential SQL ambiguity issues in ORDER BY clauses by introducing and using a new method: fully_qualified_order_by.
Changes made:
Introduced fully_qualified_order_by, which combines the quoted table and column name with sort direction.
Replaced ambiguous
order_by
references withfully_qualified_order_by
in:reorder_with_parent_id
(innumeric_order_support.rb
)with_order_option
,scope_with_order
, andhas_many_order_with_option
(insupport.rb
)Why?
tldr: If you try to use joins or subqueries in a relationship and you have a positional column with the same name the closure tree uses you are going to have, see and feel pain inflicted upon you.
This PR aims to fix this.
Suppose you have two closure trees for different things and they share a relationship;
Biz#external_categories
will trip up a"PG::AmbiguousColumn: ERROR: column reference "position" is ambiguous"
on postgresqlThe real world use case is a lot more convoluted than this, but I think I simplified it enough to be understandable to why the order expression should be fully qualified with table name and column name