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
Fix Collection cache key with limit and custom select #33075
Fix Collection cache key with limit and custom select #33075
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
8f961d4
to
a0e9e7a
Compare
a0e9e7a
to
ec43cdc
Compare
Can you add a test to make sure the issue is fixed? |
Sure! I'll work on a test |
f0e2db0
to
9d7d3c5
Compare
9d7d3c5
to
d2937b8
Compare
@rafaelfranca done! |
👍 |
Your test passes even without your patch. I pulled your patch locally, copied the test file and then checked out the commit before your patch, I then pasted in your test file. It passes
|
@schneems This happens only with postgres, can you run it with postgres adapter please ? |
activerecord/CHANGELOG.md
Outdated
@@ -33,6 +33,13 @@ | |||
|
|||
*Ryuta Kamizono* | |||
|
|||
|
|||
* Fix Collection cache key with limit and custom select (PG:AmbigousColumn: Error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This not only happen on postgres but also on mysql2, so preferable more generic changelog entry, and please add new changelog entry to top of the file.
% ARCONN=mysql2 be ruby -w -Itest test/cases/collection_cache_key_test.rb -n test_cache_key_for_relation_with_custom_select_and_limit
Using mysql2
Run options: -n test_cache_key_for_relation_with_custom_select_and_limit --seed 6336
# Running:
E
Error:ActiveRecord::CollectionCacheKeyTest#test_cache_key_for_relation_with_custom_select_and_limit:
ActiveRecord::StatementInvalid: Mysql2::Error: Duplicate column name 'updated_at': SELECT COUNT(*) AS `size`, MAX(subquery_for_cache_key.updated_at) AS timestamp FROM (SELECT developers.*, `developers`.`updated_at` FROM `developers` WHERE `developers`.`salary` = ? ORDER BY `devel
opers`.`updated_at` DESC LIMIT ?) subquery_for_cache_key
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb:116:in `prepare
'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb:116:in `block i
n exec_stmt_and_free'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:601:in `block (2 levels)
in log'
/Users/kamipo/.rbenv/versions/2.5.1/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:600:in `block in log'
/Users/kamipo/src/github.com/rails/rails/activesupport/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:591:in `log'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb:112:in `exec_st
mt_and_free'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb:39:in `exec_query'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:455:in `select'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:62:in `select_all'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:101:in `select_all'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb:12:in `select_all'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:69:in `select_one'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/collection_cache_key.rb:35:in `collection_cache_key'
/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation.rb:297:in `cache_key'
test/cases/collection_cache_key_test.rb:50:in `block in <class:CollectionCacheKeyTest>'
rails test test/cases/collection_cache_key_test.rb:45
@@ -5,7 +5,6 @@ module CollectionCacheKey | |||
def collection_cache_key(collection = all, timestamp_column = :updated_at) # :nodoc: | |||
query_signature = ActiveSupport::Digest.hexdigest(collection.to_sql) | |||
key = "#{collection.model_name.cache_key}/query-#{query_signature}" | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep a blank line.
@fedxgibson confirm this test fails on postgres. Looks good. Please address the changes by @kamipo and I think this is good to go. |
d2937b8
to
773a901
Compare
…umn: Error) Change query to use alias name for timestamp_column to avoid ambiguity problems when using timestamp from subquery.
773a901
to
b1aeae0
Compare
…y_limit_custom_select Fix Collection cache key with limit and custom select
Thanks! |
…y_limit_custom_select Fix Collection cache key with limit and custom select
Fix Collection cache key with limit and custom select (PG:AmbigousColumn: Error)
Change query to use alias name for timestamp_column to avoid ambiguity problems when using timestamp from subquery.
It fixes #33056