Fix ambiguous ORDER BY clauses by fully qualifying column names#452
Fix ambiguous ORDER BY clauses by fully qualifying column names#452eliezersouzareis wants to merge 3 commits intoClosureTree:masterfrom
Conversation
an unambiguous order by expression
…e the fully qualified column name
…with_order and has_many_order_with_option methods to use fully_qualified_order_by
|
Thanks for this PR addressing the ambiguous ORDER BY clauses. Good news - I just released v9.0.0 of closure_tree, which includes a major refactoring where I've converted many hardcoded SQL queries to use Arel (PR #453). This conversion should resolve the ambiguity issues you're experiencing, as Arel automatically handles proper table aliasing and column qualification. Could you please test your use case with v9.0.0 and let me know if the issue persists? If it does, I'd be happy to work on additional fixes. The Arel conversion specifically addresses:
You can update to v9.0.0 by running: Thanks again for identifying this issue and providing such a detailed explanation! |
|
Hi. Since you bumped both the I could, of course, fork the repo and downgrade the version requirements, but that would likely also mean adjusting the code. At that point, it would be moot, since I wouldn’t really be testing your work. That feels like more work than I can reasonably take on just to test this. Thanks for maintaining the gem and keeping it moving forward. |
|
Thank you. I could not reproduce the issue with the details you provided, so i seem that the ordering was an ActiveRecord issue or our usage of hardcoded SQL. I believe the new version since we use Arel, should not cause this issue anymore. |
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_byreferences withfully_qualified_order_byin: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_categorieswill 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