You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When calling #include? on an ActiveRecord::Relation that has an aliased select value AND a having condition that references said aliased select value, an ActiveRecord::StatementInvalid error is raised due to not being able to find the aliased column.
Example query that meets these criteria would be:
Author.select('COUNT(*) as total_posts','authors.*').joins(:posts).group(:id).having('total_posts > 2').include?(Author.first)
This problem appears to have been introduced with the following commit: 166b63e
Steps to reproduce
# frozen_string_literal: truerequire"bundler/inline"gemfile(true)dosource"https://rubygems.org"git_source(:github){ |repo| "https://github.com/#{repo}.git"}# Activate the gem you are reporting the issue against.#gem "activerecord", "6.0.3.4" # no exception raised#gem "activerecord", "6.1.2" # exception from 6.1.x onwardsgem"rails",github: "rails/rails",branch: "main"gem"sqlite3"endrequire"active_record"require"minitest/autorun"require"logger"# This connection will do for database-independent bug reports.ActiveRecord::Base.establish_connection(adapter: "sqlite3",database: ":memory:")ActiveRecord::Base.logger=Logger.new(STDOUT)ActiveRecord::Schema.definedocreate_table:posts,force: truedo |t|
endcreate_table:comments,force: truedo |t|
t.integer:post_idt.integer:user_idendcreate_table:users,force: truedo |t|
endendclassPost < ActiveRecord::Basehas_many:commentsscope:active,->{select('COUNT(*) as total_comments, posts.id').joins(:comments).group(:id).having('total_comments > 3')}endclassComment < ActiveRecord::Basebelongs_to:postbelongs_to:userendclassBugTest < Minitest::Testdeftest_exception_should_not_be_raisedpost1=Post.create!post2=Post.create!5.timesdopost1.comments << Comment.create!end3.timesdopost2.comments << Comment.create!endassert_equal5,post1.comments.countassert_equal3,post2.comments.count# Raises exceptionassert_equaltrue,Post.active.include?(post1)assert_equalfalse,Post.active.include?(post2)endend
Expected behavior
No exception should be raised, and true or false should be returned based on whether or not the relation contains the record given as parameter
Actual behavior
Error:
BugTest#test_exception_should_not_be_raised:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: total_comments
/var/lib/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `initialize'
/var/lib/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `new'
/var/lib/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `prepare'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb:46:in `block (2 levels) in exec_query'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activesupport/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activesupport/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activesupport/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb:43:in `block in exec_query'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:724:in `block (2 levels) in log'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activesupport/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activesupport/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activesupport/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activesupport/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activesupport/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:723:in `block in log'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activesupport/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:714:in `log'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb:42:in `exec_query'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:547:in `select'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `select_all'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:110:in `select_all'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:91:in `select_rows'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/relation/finder_methods.rb:346:in `block in exists?'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/relation.rb:890:in `skip_query_cache_if_necessary'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/relation/finder_methods.rb:346:in `exists?'
/home/vagrant/.bundle/ruby/2.7.0/rails-9eedc375d909/activerecord/lib/active_record/relation/finder_methods.rb:358:in `include?'
include_on_having_query_test.rb:67:in `test_exception_should_not_be_raised'
System configuration
Rails version: 6.1.2, main
Ruby version: 2.7.1
Will try my hand at a pull request to fix the issue.
The text was updated successfully, but these errors were encountered:
When calling
#include?
on anActiveRecord::Relation
that has an aliased select value AND a having condition that references said aliased select value, anActiveRecord::StatementInvalid
error is raised due to not being able to find the aliased column.Example query that meets these criteria would be:
This problem appears to have been introduced with the following commit: 166b63e
Steps to reproduce
Expected behavior
No exception should be raised, and true or false should be returned based on whether or not the relation contains the record given as parameter
Actual behavior
System configuration
Rails version: 6.1.2, main
Ruby version: 2.7.1
Will try my hand at a pull request to fix the issue.
The text was updated successfully, but these errors were encountered: