gpt4 book ai didi

mysql - Rails 3.2 + ruby​​2 + mysql2 + NoMethodError : undefined method `query' for nil:NilClass

转载 作者:行者123 更新时间:2023-11-29 06:42:28 30 4
gpt4 key购买 nike

我们正在升级到
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)

如果我遗漏了什么,请告诉我。

最佳答案

确保你把:

ActiveRecord::Base.establish_connection YOUR_DB

'up' 或 'change' 方法之外的行。以便在初始化迁移类时而不是在方法中调用它。我猜错误的发生是因为没有足够的时间建立连接。

如:

class AddBlahToBlah < ActiveRecord::Migration
ActiveRecord::Base.establish_connection "#{Rails.env}_foo"

def up
create_table ...
end
end

关于mysql - Rails 3.2 + ruby​​2 + mysql2 + NoMethodError : undefined method `query' for nil:NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20660191/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com