我们正在升级到 rails版本3.2.16。
Mysql gem:mysql2(0.3.14)
ruby 2.0.0p247
当我运行rake db:migrate时,我遇到异常:
NoMethodError: undefined method `query' for nil:NilClass: CREATE TABLE `recently_viewed_products` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `gid` bigint NOT NULL, `product_id` int(11) NOT NULL, `viewed_time` int(11)) ENGINE=InnoDB/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:246:in `block in execute'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.16/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/lib/ruby/gems/2.0.0/gems/ar-octopus-0.8.0/lib/octopus/rails3/abstract_adapter.rb:19:in `instrument'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:246:in `execute'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/mysql2_adapter.rb:213:in `execute'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:435:in `create_table'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:466:in `block in method_missing'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:438:in `block in say_with_time'
/usr/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:438:in `say_with_time'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:458:in `method_missing'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:334:in `method_missing'
/mnt/xxx/releases/20131218044833/db/migrate/20131202073728_create_recently_viewed_products.rb:5:in `up'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:370:in `up'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/usr/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:410:in `block in migrate'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:389:in `migrate'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:528:in `migrate'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:777:in `call'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:777:in `ddl_transaction'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:719:in `block in migrate'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:700:in `each'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:700:in `migrate'
/usr/lib/ruby/gems/2.0.0/gems/ar-octopus-0.8.0/lib/octopus/migration.rb:82:in `migrate_with_octopus'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:570:in `up'
/usr/lib/ruby/gems/2.0.0/gems/ar-octopus-0.8.0/lib/octopus/migration.rb:105:in `up_with_octopus'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:551:in `migrate'
/usr/lib/ruby/gems/2.0.0/gems/ar-octopus-0.8.0/lib/octopus/migration.rb:97:in `migrate_with_octopus'
/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/usr/bin/rake:23:in `load'
/usr/bin/rake:23:in `<main>'
仅在运行非默认数据库的迁移时才会出现异常。 这是我的迁移:
ActiveRecord::Base.establish_connection("big_#{Rails.env}")
create_table :recently_viewed_products do |t|
t.integer :gid, :null => false, :limit => 8
t.integer :product_id, :null => false
t.integer :viewed_time
end
add_index :recently_viewed_products, :gid
add_index :recently_viewed_products, [:gid, :product_id], :unique=>true, :name => "UNIQUE"
ActiveRecord::Base.establish_connection(Rails.env)
如果我遗失了任何东西,请告诉我。
答案 0 :(得分:2)
确保你输入:
ActiveRecord::Base.establish_connection YOUR_DB
在'up'或'change'方法之外的行。因此,在初始化迁移类而不是在方法中调用它。我猜错误的发生是因为没有足够的时间建立连接。
EG:
class AddBlahToBlah < ActiveRecord::Migration
ActiveRecord::Base.establish_connection "#{Rails.env}_foo"
def up
create_table ...
end
end