This project is archived and is in readonly mode.
incorrect sql from has_many :through
-
Dan Pickett
- Tag changed from :through, :through_conditions, has_many, has_many_through to :through, :through_conditions, bugmash, has_many, has_many_through
Can a bugmasher verify this behavior with a failing test case in master?
-
Neeraj Singh
I am not able to reproduce this issue in rails edge.
-
Diego Algorta
-1 not reproducible
This ticket is not fully coherent to me. What are you wanting to get with FleetAgent.count(:include=> [:position_mappings])
which would be any different from a plain FleetAgent.count ? -
Enrico Bianco
+1 verified in current master.
Here are my models:
class User < ActiveRecord::Base has_many :position_mappings endclass PositionMapping < ActiveRecord::Base belongs_to :user endclass FleetAgent < ActiveRecord::Base belongs_to :user has_many :position_mappings, :through => :user endAnd here's the error:
ruby-1.8.7-p249 > FleetAgent.count(:include => [:position_mappings]) ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'users.user_id' in 'on clause': SELECT COUNT(DISTINCTfleet_agents.id) AS count_id FROMfleet_agentsLEFT OUTER JOINusersONfleet_agents.id=users.user_idLEFT OUTER JOINposition_mappingsONposition_mappings.user_id=users.idBut, strangely, the error doesn't happen when calling for actual models instead of counting:
(Note: I didn't actually write any records into the DB)ruby-1.8.7-p249 > FleetAgent.all(:include => [:position_mappings]) => []But why would one want to count FleetAgents and include the position_mappings relation in it? To create conditions based on the position_mappings for the count?
-
Wildgoose
Yes, haven't repro'd this with latest rails, but I think the original issue only occurred once you tried to set any kind of conditions on the relation, it's not the relation itself that see's the problem. So .count is just one way to repro it, but also doing any kind of filter I think should also cause the same issue?
I think if you glance at the code I referenced in the original error report (and presumably factor in that it might have moved line since), then the problem was fairly easy to see - there are several code paths through that section and quite clearly you can see that several types of association should be handled, but there is code only for I think "has_many" associations?
I think if you look at the code the problem should be quite apparent? The challenge to code a fix is that I wasn't sure what other code paths enter this hunk and hence what the full and proper fix should look like. It's somewhat abstract at this point what the various variables point to in terms of different types of relationships and I think it would take someone a little more familiar with what else uses this code to do a full fix (ie we should cover all types of relationship here). I don't think the change is hard or a big change, it's simply that the code appears to be used by several other (unknown) code paths and the solution looks a bit subtle?
From memory it's only a problem when you apply a relationship which is probably why not so many people bang into this?
-
Jon Leighton
- Assigned user set to Aaron Patterson
- Tag changed from :through, :through_conditions, bugmash, has_many, has_many_through to :through, :through_conditions, bugmash, has_many, has_many_through, patch
I can confirm this. It affects queries involving joins, so while the above example using
countdoesn't quite seem to make sense, a query in the formFoo.joins(:bar)would equally have this problem.Attached is a fix, with tests.
-
Jon Leighton
Just realised the file name of that patch says 2461. This is a mistake, but rest assured the patch is definitely concerned with this ticket and not a different one.
-
Repository
- State changed from new to resolved
(from [b8153fd5a18441567f787a33ca882acb3bb5088a]) Fix problem where wrong keys are used in JoinAssociation when an association goes :through a belongs_to [#2801 incorrect sql from has_many :through state:resolved] https://github.com/rails/rails/commit/b8153fd5a18441567f787a33ca882...
-
Jon Leighton
- Milestone cleared.
- State changed from resolved to open
I've seen about 3 other bug reports about this issue, so I've ported the fix to 3-0-stable too, as it would be good to get in 3.0.4. Thanks.
-
Repository
- State changed from open to resolved
(from [e4b384222c303414c42d92c67acdc87385c49c92]) Fix problem where wrong keys are used in JoinAssociation when an association goes :through a belongs_to [#2801 incorrect sql from has_many :through state:resolved] https://github.com/rails/rails/commit/e4b384222c303414c42d92c67acdc...
