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
ActiveRecord query using where() with an array results in SQL without "IN" clause. #41133
Comments
Hey @key88sf I'm not sure what you mean by:
The SQL you pasted contains an IN clause: The reason you're seeing |
👍 Thanks for the explanation. Looking forward to the bugfix. Will close this issue. |
Related issue rails#41133. In rails#39106, I've allowed binds of casted values as an alternative of the legacy binds (an array of `[column || nil, value]`) to deprecate and remove the legacy binds support in a future version of Rails. At that time, I've respected the existing logging format for binds. i.e. ```ruby # legacy binds conn.select_all("SELECT * FROM events WHERE id IN (?, ?)", nil, [[nil, 1], [nil, 2]]) # (0.1ms) SELECT * FROM events WHERE id IN (?, ?) [[nil, 1], [nil, 2]] # casted binds conn.select_all("SELECT * FROM events WHERE id IN (?, ?)", nil, [1, 2]) # (0.1ms) SELECT * FROM events WHERE id IN (?, ?) [[nil, 1], [nil, 2]] ``` To improve the performance of generating IN clause, 72fd0ba avoids `build_bind_attribute` for each values, so now binds has become casted values. ```ruby conn.select_all(Event.where(id: [1, 2])) # (0.1ms) SELECT * FROM events WHERE id IN (?, ?) [[nil, 1], [nil, 2]] ``` Regardless of whether 72fd0ba avoids `build_bind_attribute` or not, the logging format for the binds of casted values is odd (at least not pretty to me). I'd like to concise the logging format to just use casted values. ```ruby conn.select_all("SELECT * FROM events WHERE id IN (?, ?)", nil, [1, 2]) # (0.1ms) SELECT * FROM events WHERE id IN (?, ?) [1, 2] conn.select_all(Event.where(id: [1, 2])) # (0.1ms) SELECT * FROM events WHERE id IN (?, ?) [1, 2] ```
Steps to reproduce
With any standard ActiveRecord model (using
Customer
here as an example), issue the AR query:Customer.find([1, 10])
Expected behavior
I would expect the generated SQL to look like:
SELECT * FROM customers WHERE (customers.id IN (1,2))
(this is also what the Rails Guide lists: https://guides.rubyonrails.org/active_record_querying.html)
Actual behavior
I get this SQL instead:
SELECT customers.* FROM customers WHERE customers.id IN ($1, $2) [[nil, 1], [nil, 2]]
It does work, but when using an array with a lot of elements, this expands out to a query with many variables being substituted. Is this the intended behavior???
The same SQL is generated when using a
.where(id: [1,2])
syntax as well.System configuration
Rails version:
6.1
Ruby version:
2.6.3
The text was updated successfully, but these errors were encountered: