Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::StatementInvalid. PG Error

I am trying to find a project from Project model using Project.find(id) but it is giving me ActiveRecord::StatementInvalid error

Full trace-

 PG::Error: ERROR: prepared statement "a1" already exists : SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind in ('v','r') AND c.relname = $1 AND n.nspname = ANY (current_schemas(false))
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1180:in `prepare'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1180:in `prepare_statement'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1144:in `exec_cache'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-     3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:664:in `block in exec_query'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activesupport-3.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in `exec_query'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:797:in `table_exists?'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/schema_cache.rb:30:in `table_exists?'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:223:in `table_exists?'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/attribute_methods/primary_key.rb:75:in `get_primary_key'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/attribute_methods/primary_key.rb:60:in `reset_primary_key'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/attribute_methods/primary_key.rb:49:in `primary_key'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:230:in `block in columns'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `map'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `columns'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:237:in `columns_hash'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/relation/delegation.rb:7:in `columns_hash'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/relation/finder_methods.rb:330:in `find_one'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/relation/finder_methods.rb:311:in `find_with_ids'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/relation/finder_methods.rb:107:in `find'
/home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/querying.rb:5:in `find'
/home/deploy/submit_contactpl/app/workers/php_worker.rb:5:in `perform'

Line 5 of php_worker is

 project = Project.find(project_id)

What might be the issue? Could you shade some light on it. What might be the possible solution

like image 860
Bhushan Lodha Avatar asked Apr 16 '12 08:04

Bhushan Lodha


2 Answers

all it needed was

 task "resque:setup" => :environment do
  #ENV['QUEUE'] = '*'

  Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
 end

in your resque.rake file

like image 156
Bhushan Lodha Avatar answered Nov 08 '22 11:11

Bhushan Lodha


It's a bug somewhere in your Ruby stack.

It's trying to prepare a statement called "a1" which is examining table/view definitions (presumably for your "project" table). It finds out a prepared statement with the same name already exists and spits out an error. The query it is trying to prepare is fine - it returns a count of how many tables have a given name.

Either it's not tracking its prepared statements properly or it thinks it's deleted one and hasn't really. I'd update rails & activerecord and check for bug reports.

like image 20
Richard Huxton Avatar answered Nov 08 '22 12:11

Richard Huxton