-
-
Notifications
You must be signed in to change notification settings - Fork 780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect use of table alias when joining with same table twice (Rails 6.0.2.2 issue) #1119
Comments
Update: Figured out this issue was introduced even before Rails 6.0.2.2, specifically in Rails 6.0.1. https://github.com/rails/rails/releases/tag/v6.0.1 Issue is still present in Rails 6.0.3.1 |
Error can be observed by adding |
Thanks @PhilCoggins this was helpful! I've investigated this a bit further and noticed something. Here are two test examples you should add to https://github.com/activerecord-hackery/ransack/blob/master/spec/ransack/search_spec.rb I've managed to get the same setup with your test models. Passing spec:
However, if I change the order of conditions this test starts to fail and you get the same invalid SQL generated as in my initial example:
to
Failing spec:
Invalid SQL
Same culprit as in my initial example:
|
Nice find, I think the culprit can be found in rails/rails#37235, which fixed inconsistent join ordering in ActiveRecord. If there's a way to pass joins in the proper order from Ransack to ActiveRecord, I think that would fix this. |
Looks like these are a lot of the same issues observed in #1081 |
Does rails/rails#39305 help? |
Sounds super promising! |
I've tried rails/rails#39305 that @varyonic suggested and I've run the failing test again and it seems like the query isn't invalid anymore:
After:
Query itself is valid and uses correct aliases in JOIN and WHERE part. 🎉 My failing test is still failing, but with different error and it is actually supposed to fail in this case because the JOINs are in different order, depending on the order of given conditions, which is how it's supposed to work. Point of my failing test was to catch invalid query you get when you change the order of given conditions. |
@nikajukic could you please rebase and see if the problem still exists? |
I have faced a similar issue with Rails 6.0.2 and Ransack 2.4.0 After upgrading Rails version to 6.1.0 it all works well |
Wow that is unexpected good news!! |
@seanfcarroll I'll upgrade to Rails 6.1 in the next few days and put an update here. |
@seanfcarroll I've successfully upgraded Rails to 6.1.1 and ransack to 2.4.1 and this issue is now fixed 🎉 |
Hey that is great news @nikajukic thanks for posting back here! |
Ruby 2.6.1
Ransack 2.3.2
I tried using master branch as well and got the same issue: 6434e3b
I have an issue with generating queries on Rails 6.0.2.2.
With Rails 6.0.0. everything worked fine.
Issue happens when you try to join with same table twice.
Query:
Track.ransack(song_title_cont_any: 'iva', song_artist_name_cont_any: 'cold', producer_name_cont_any: 'artin').result
In Rails 6.0.0. it generates correct query. Notice how producer_tracks alias is used in both JOIN and WHERE.
In Rails 6.0.2.2 it generates incorrect query and fails with PG::UndefinedTable: ERROR error:
Notice how:
artists_songs
alias in JOIN part, but doesn't use it in WHERE partproducers_tracks
alias in JOIN part, but uses it in WHERE partThis query fails with following error:
The text was updated successfully, but these errors were encountered: