kostyantyn / hydra_attribute

Implementation of EAV pattern for ActiveRecord models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

model.count in postgres 9.1

LAlexey opened this issue · comments

I have a model, i.e.

class Contest < ActiveRecord::Base
include HydraAttribute::ActiveRecord
end

Then I with to get Contest.count from rails console and get the following error:

1.9.3p327 :001 > Contest.count
HydraAttribute::HydraAttribute Load (0.6ms) SELECT "hydra_attributes".* FROM "hydra_attributes" WHERE "hydra_attributes"."entity_type" = 'Contest'
(0.3ms) SELECT COUNT(), "contests"."id", "contests"."hydra_set_id" FROM "contests"
ActiveRecord::StatementInvalid: PG::Error: ERROR: column "contests.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT COUNT(
), "contests"."id", "contests"."hydra_set_id" ...
^
: SELECT COUNT(*), "contests"."id", "contests"."hydra_set_id" FROM "contests"
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in async_exec' from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:inexec_no_cache'
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in block in exec_query' from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:280:inblock in log'
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in instrument' from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:275:inlog'
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in exec_query' from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1248:inselect'
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in select_all' from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/query_cache.rb:63:inselect_all'
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in select_one' from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:30:inselect_value'
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:243:in execute_simple_calculation' from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:208:inperform_calculation'
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:159:in calculate' from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:58:incount'
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/querying.rb:12:in count' from (irb):1 from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:instart'
from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in start' from /home/alexeyl/startup/brandkiller/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in<top (required)>'
from script/rails:6:in require' from script/rails:6:in

'

It happens due to id and hydra_set_id fields.
In module HydraAttribute::ActiveRecord::Relation::QueryMethods, build_arel method:

id and hydra_set_id always added to select.
"attributes "id" and "hydra_set_id" are important for hydra_attribute gem"
if @hydra_select_values.any? or @select_values.any?
@select_values << hydra_helper.prepend_table_name(klass.primary_key)
@select_values << hydra_helper.prepend_table_name('hydra_set_id')
end

Is there any workaround?

Thank you for finding a bug. I've released a new version with a fix.
Currently all tests are written for SQLite, so this error was not noticed. I am going to set up test environments for MySQL and PostgreSQL databases to prevent the similar errors in the future.