我有迁移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 cause output: ** Invoke db:migrate (first_time) ** Invoke db:environment (first_time) ** Execute db:environment ** Invoke db:load_config (first_time) ** Execute db:load_config ** Execute db:migrate == 20150930051523 CreateObjects: migrating ==================================== -- create_table(:objects) -> 0.0010s rake aborted! StandardError: An error has occurred, this and all later migrations canceled: uninitialized constant 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 `migrate' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in `migrate' /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:in `transaction' /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 `migrate' /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 `migrate' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:in `migrate' /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: uninitialized constant 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 `migrate' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in `migrate' /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:in `transaction' /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 `migrate' /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 `migrate' /var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:in `migrate' /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 `' Tasks: TOP => db:migrate
在Object.create ......阶段出现错误,出了什么问题?
答案 0 :(得分:0)
更新:在评论中指出实际代码不包含“对象”,因此这个答案基于错误的例子。
Object
是一个用于(unscoped)Model类的错误名称,因为它已经由Ruby定义。
您调用create Object
可能不是您在模型源中定义的Object
类。
我假设你运行rails。从迁移中删除Object.create
行。运行它,它应该工作正常。然后,拨打rails console
,最后进入直播会话。在这里,您可以使用您的代码,例如:致电Object.create
,Object.where{id: 1}
等我认为你会得到同样的错误。
因此,作为一种解决方案,请将您的Object
重命名为其他内容,例如Class
并查看其是否有效。
好的,这是个玩笑,当然Class
对于模型类来说也是一个非常糟糕的名字:)。试着找到更有意义的东西。
答案 1 :(得分:0)
问题是通过设置pg gem来解决的,它会拉出另一个宝石和包(特别是postgresql-server-dev-all)。安装后,rake db:migrate命令可以正常工作。