Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Changing Relation#from now resets #select as well #14049

Closed
dhh opened this Issue · 5 comments

3 participants

David Heinemeier Hansson Maximiliano Guzenski Rafael Mendonça França
David Heinemeier Hansson
Owner

Rails 4.1.0.beta1:

irb(main):001:0> SignalId::User.all.from("signal_users").to_sql
=> "SELECT `signal_users`.* FROM signal_users"
irb(main):002:0> SignalId::User.all.to_sql
=> "SELECT `signal_users`.* FROM `signal_users`"

rails/master:

irb(main):005:0> SignalId::User.all.from("signal_users").to_sql
=> "SELECT * FROM signal_users"
irb(main):006:0> SignalId::User.all.to_sql
=> "SELECT `signal_users`.* FROM `signal_users`"

Any reason for this change? Doesn't seem like changing #from should force a change in #select?

Our use case is that we've added this module:

module UseIndex
  extend ActiveSupport::Concern

  module ClassMethods
    def use_index(name)
      from "#{quoted_table_name} USE INDEX (#{name})"
    end
  end
end

So calling #use_index on anything now resets the #select, which gives us very different query results.

/cc @tenderlove @jonleighton

David Heinemeier Hansson dhh added this to the 4.1.0 milestone
Maximiliano Guzenski

I dont know if this is same issue, but I have a scope with a custom "select" and I some times I use it to make a 'count'... a very simple example:

Rails 3.2:

[1] pry(main)> Notification.select('id, created_at').count
   (1.4ms)  SELECT COUNT(*) FROM "notifications" 

Rails 4.1.beta1

pry(main)> Notification.select('id, created_at').count
   (0.7ms)  SELECT COUNT(id, created_at) FROM "notifications"
PG::UndefinedFunction: ERROR:  function count(integer, timestamp without time zone) does not exist
LINE 1: SELECT COUNT(id, created_at) FROM "notifications"
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
: SELECT COUNT(id, created_at) FROM "notifications"
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  function count(integer, timestamp without time zone) does not exist
LINE 1: SELECT COUNT(id, created_at) FROM "notifications"

This is the expected behavior?

Rafael Mendonça França

@maxguzenski I don't think so.

@dhh I know what broke it. I'll fix. Thank you for reporting. 847e9a9

Rafael Mendonça França rafaelfranca self-assigned this
Rafael Mendonça França

@dhh could you provide more information how reproduce this select reset?

I tried to reproduce it but when you explicitly define the select the select values are kept.

relation = Post.first.comments
Comment.select(:body).from(relation).to_sql
# => "SELECT \"comments\".\"body\" FROM (SELECT \"comments\".* FROM \"comments\"  WHERE \"comments\".\"post_id\" = ?) subquery"
David Heinemeier Hansson
Owner
Rafael Mendonça França

Thanks I know how to write a test case now.

Rafael Mendonça França rafaelfranca closed this issue from a commit
Rafael Mendonça França rafaelfranca Revert "Merge pull request #13344 from ccutrer/fix-from-default-select"
This reverts commit 3ea8403, reversing
changes made to e4cde5d.

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/relation/query_methods.rb

Reason: using `from` without `select` should not change the select list
to SELECT * because it can lead different query results. If it is needed
to change the table to a subquery or a view you can pass a table alias
in the `from` call or use `select('subquery.*')`.

Fixes #14049.
a2075f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.