Thursday, 15 January 2015

Mysql query not working in postgresql in rails 3.2.11 -



Mysql query not working in postgresql in rails 3.2.11 -

i using next query in rails

grouped_link_counters = uattachment).link_counters.group("year(created_at), month(created_at)").count("created_at")

and gives next result in rails console using mysql adapter.

userattachment load (0.2ms) select `user_attachments`.* `user_attachments` `user_attachments`.`id` = 132 limit 1 (0.2ms) select count(`link_counters`.`created_at`) count_created_at, year(created_at), month(created_at) year_created_at_month_created_at `link_counters` `link_counters`.`user_attachment_id` = 132 grouping year(created_at), month(created_at) => {11=>9, 12=>15, 1=>1, 2=>1}

but when utilize postgresql same command throws next error :-

select count("link_counters"."created_at") count_created_at, year(created_at), month(created_at) year_created_at_month_created_at "link_counters" "link_counters"."user_attachment_id" = 1 grouping year(created_at), month(created_at) activerecord::statementinvalid: pg::error: error: function year(timestamp without time zone) not exist line 1: ...link_counters"."created_at") count_created_at, year(creat... ^ hint: no function matches given name , argument types. might need add together explicit type casts. : select count("link_counters"."created_at") count_created_at, year(created_at), month(created_at) year_created_at_month_created_at "link_counters" "link_counters"."user_attachment_id" = 1 grouping year(created_at), month(created_at) /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `async_exec' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `exec_no_cache' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in `block in exec_query' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in `instrument' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `exec_query' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1248:in `select' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:289:in `execute_grouped_calculation' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:206:in `perform_calculation' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:159:in `calculate' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:58:in `count' (irb):63 /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start' /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>' script/rails:6:in `require' script/rails:6:in `<main>'1.9.3p194 :064 >

can tell me can done same response postgresql. in advance!

in postgresql, don't have helper functions year, month etc. instead of these, can utilize date_part in postgresql same result mysql.

e.g.

grouped_link_counters = uattachment.link_counters.group("date_part('year', created_at), date_part('month', created_at)").count("created_at")

mysql ruby-on-rails-3 postgresql activerecord

No comments:

Post a Comment