rake db:migrate 导致 StandardError - 未初始化的常量 CreateObjects::Object

Posted

技术标签:

【中文标题】rake db:migrate 导致 StandardError - 未初始化的常量 CreateObjects::Object【英文标题】:rake db:migrate cause StandardError - uninitialized constant CreateObjects::Object 【发布时间】:2015-12-27 21:16:45 【问题描述】:

我有迁移 20150930051523_create_objects.rb:

class CreateObjects < ActiveRecord::Migration
def change
  create_table :objects do |t|
    t.text :name
    t.timestamps null: false
  end

  Object.create :name => "A"
  Object.create :name => "B"
  Object.create :name => "C"

  end
end
$ rake:db migrate --trace 导致输出: ** 调用 db:migrate (first_time) ** 调用 db:environment (first_time) ** 执行数据库:环境 ** 调用 db:load_config (first_time) ** 执行 db:load_config ** 执行数据库:迁移 == 20150930051523 CreateObjects:迁移 ==================================== -- create_table(:objects) -> 0.0010 秒 耙中止! StandardError:发生错误,此迁移和所有后续迁移已取消: 未初始化的常量 CreateObjects::Object/db/migrate/20150930051523_create_objects.rb:8:in `change' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:605:in `exec_migration' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:589:in `block (2 levels) in migrate' /usr/lib/ruby/2.1.0/benchmark.rb:279:in `measure' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:588:in `block in migrate' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:587:in `迁移' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in `迁移' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:1041:in `block in ddl_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:在“事务”中 /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:220:in `transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:1041:in `ddl_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:994:in `execute_migration_in_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:956:in `block in migrate' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in `each' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in `迁移' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:820:in `up' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:798:in `迁移' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:在“迁移”中 /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:44:in `block (2 levels) in ' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain' /usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run' /var/lib/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in `' /usr/local/bin/rake:23:in `load' /usr/local/bin/rake:23:in `' NameError:未初始化的常量 CreateObjects::Object/db/migrate/20150930051523_create_barbers.rb:8:in `change' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:605:in `exec_migration' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:589:in `block (2 levels) in migrate' /usr/lib/ruby/2.1.0/benchmark.rb:279:in `measure' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:588:in `block in migrate' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:587:in `迁移' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in `迁移' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:1041:in `block in ddl_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:在“事务”中 /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:220:in `transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:1041:in `ddl_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:994:in `execute_migration_in_transaction' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:956:in `block in migrate' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in `each' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in `迁移' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:820:in `up' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:798:in `迁移' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:在“迁移”中 /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:44:in `block (2 levels) in ' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain' /usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling' /var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run' /var/lib/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in `' /usr/local/bin/rake:23:in `load' /usr/local/bin/rake:23:in `' 任务:TOP => db:migrate

在 Object.create... 阶段出现错误,出了什么问题?

【问题讨论】:

【参考方案1】:

更新:在评论中指出实际代码不包含“对象”,因此此答案基于错误的示例。

Object 是一个不好的名称,不适用于(无范围的)模型类,因为它已经由 Ruby 定义。

您调用 create 的 Object 可能不是您在模型源中定义的 Object 类。

我假设你运行 rails。从迁移中删除 Object.create 行。运行它,它应该可以正常工作。然后,致电rails console,您将进入实时会话。在这里你可以玩弄你的代码,例如致电Object.createObject.whereid: 1 等。我假设您会遇到同样的错误。

因此,作为一种解决方案,将您的 Object 重命名为其他名称,例如 Class 并查看它是否有效。

好吧,那是个玩笑,当然Class 对于模型类来说也是一个非常糟糕的名字:)。尝试寻找更有意义的东西。

【讨论】:

Object 是一个模板词,我用它来代替 Barber & Barbers: class CreateBarbers "Jessie Pinkman" Barber.create :name => "Walter White" Barber.create :name => "Gus Fring" end end 那么请在您的问题中提供实际代码...! 谢谢,Felix,问题已通过设置 pg gem 解决,它会拉取另一个 gem 和包(特别是 postgresql-server-dev-all)。安装后,命令 rake db:migrate 可以正常工作。 @irishfromua 很高兴听到。在 ***,将已解决的答案标记为这样很重要(这不是一劳永逸,更像是***),所以请更新您的问题,自己发布答案并接受它。谢谢。并确保阅读此处的帮助部分(右上角)。【参考方案2】:

问题已通过设置 pg gem 解决,该 gem 会拉取另一个 gem 和包的捆绑包(特别是 postgresql-server-dev-all)。安装后,命令 rake db:migrate 工作正常。

【讨论】:

以上是关于rake db:migrate 导致 StandardError - 未初始化的常量 CreateObjects::Object的主要内容,如果未能解决你的问题,请参考以下文章

markdown rake db:reset,rake db:migrate:resetの违い

如何运行 db:migrate 从另一个带参数的 rake 任务?

rake db:migrate不执行任何操作

如何使用 rake db:migrate 仅回滚一步

在 Heroku 上执行 rake db:migrate 时出错

使用 rake db:migrate 迁移数据不会改变它