rake migration aborted:找不到表'role'

时间:2012-10-02 22:05:06

标签: ruby-on-rails migration rake abort

我刚刚继承了我正在尝试运行迁移的代码,但我不断收到rake aborted错误。我遇到过其他似乎有类似问题的人,但大部分涉及到Heroku而我正试图在本地运行(开始。)

我尝试使用PostgreSQL和SQLite进行故障排除,两者都产生同样的问题。引用的表“角色”是第二次调用的迁移,所以我很难搞清楚导致它无法构建的原因。非常感谢任何和所有的帮助。提前谢谢。

以下是角色迁移:

class CreateRoles < ActiveRecord::Migration
  def change
    create_table :roles do |t|
      t.string :name

      t.timestamps
    end
  end
end

以下是SQLite的跟踪:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
Could not find table 'roles'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/sqlite_adapter.rb:470:in `table_structure'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/sqlite_adapter.rb:351:in `columns'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:228:in `yield'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:228:in `default'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:228:in `columns'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:248:in `column_names'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:261:in `column_methods_hash'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/dynamic_matchers.rb:69:in `all_attributes_exists?'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/dynamic_matchers.rb:27:in `method_missing'
/Users/sa/Documents/AptanaWorkspace/recprototype/config/initializ
ers/constants.rb:1:in `<top (required)>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:245:in `load'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:245:in `block in load'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:236:in `load_dependency'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:245:in `load'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/engi
ne.rb:588:in `block (2 levels) in <class:Engine>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/engi
ne.rb:587:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/engi
ne.rb:587:in `block in <class:Engine>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:30:in `instance_exec'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:30:in `run'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:55:in `block in run_initializers'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:54:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:54:in `run_initializers'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/appl
ication.rb:136:in `initialize!'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/rail
tie/configurable.rb:30:in `method_missing'
/Users/sa/Documents/AptanaWorkspace/recprototype/config/environme
nt.rb:5:in `<top (required)>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:251:in `require'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:251:in `block in require'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:236:in `load_dependency'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:251:in `require'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/appl
ication.rb:103:in `require_environment!'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/appl
ication.rb:292:in `block (2 levels) in initialize_tasks'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:205:in `call'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:205:in `block in execute'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:200:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:200:in `execute'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:158:in `block in invoke_with_call_chain'
/Users/sa/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/monitor.rb:201:in
 `mon_synchronize'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:151:in `invoke_with_call_chain'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:176:in `block in invoke_prerequisites'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:174:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:174:in `invoke_prerequisites'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:157:in `block in invoke_with_call_chain'
/Users/sa/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/monitor.rb:201:in
 `mon_synchronize'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:151:in `invoke_with_call_chain'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:144:in `invoke'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:116:in `invoke_task'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:94:in `block (2 levels) in top_level'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:94:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:94:in `block in top_level'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:133:in `standard_exception_handling'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:88:in `top_level'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:66:in `block in run'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:133:in `standard_exception_handling'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:63:in `run'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/bin/rake:33:in `
<top (required)>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/bin/rake:19:in `load'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/bin/rake:19:in `<main>'
Tasks: TOP => db:migrate => environment

以下是PostgreSQL的跟踪:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
PG::Error: ERROR:  relation "roles" does not exist
LINE 4:              WHERE a.attrelid = '"roles"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a
.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"roles"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/postgresql_adapter.rb:1106:in `async_exec'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/postgresql_adapter.rb:1106:in `exec_no_cache'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/postgresql_adapter.rb:650:in `block in exec_query'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/notifications/instrumenter.rb:20:in `instrument'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/postgresql_adapter.rb:649:in `exec_query'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/postgresql_adapter.rb:1231:in `column_definitions'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/postgresql_adapter.rb:845:in `columns'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:228:in `yield'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:228:in `default'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:228:in `columns'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:248:in `column_names'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/model_schema.rb:261:in `column_methods_hash'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/dynamic_matchers.rb:69:in `all_attributes_exists?'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.1/lib/active
_record/dynamic_matchers.rb:27:in `method_missing'
/Users/sa/Documents/AptanaWorkspace/recprototype/config/initializ
ers/constants.rb:1:in `<top (required)>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:245:in `load'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:245:in `block in load'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:236:in `load_dependency'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:245:in `load'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/engi
ne.rb:588:in `block (2 levels) in <class:Engine>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/engi
ne.rb:587:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/engi
ne.rb:587:in `block in <class:Engine>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:30:in `instance_exec'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:30:in `run'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:55:in `block in run_initializers'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:54:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/init
ializable.rb:54:in `run_initializers'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/appl
ication.rb:136:in `initialize!'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/rail
tie/configurable.rb:30:in `method_missing'
/Users/sa/Documents/AptanaWorkspace/recprototype/config/environme
nt.rb:5:in `<top (required)>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:251:in `require'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:251:in `block in require'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:236:in `load_dependency'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.1/lib/activ
e_support/dependencies.rb:251:in `require'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/appl
ication.rb:103:in `require_environment!'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.1/lib/rails/appl
ication.rb:292:in `block (2 levels) in initialize_tasks'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:205:in `call'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:205:in `block in execute'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:200:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:200:in `execute'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:158:in `block in invoke_with_call_chain'
/Users/sa/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/monitor.rb:201:in
 `mon_synchronize'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:151:in `invoke_with_call_chain'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:176:in `block in invoke_prerequisites'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:174:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:174:in `invoke_prerequisites'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:157:in `block in invoke_with_call_chain'
/Users/sa/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/monitor.rb:201:in
 `mon_synchronize'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:151:in `invoke_with_call_chain'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb
:144:in `invoke'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:116:in `invoke_task'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:94:in `block (2 levels) in top_level'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:94:in `each'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:94:in `block in top_level'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:133:in `standard_exception_handling'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:88:in `top_level'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:66:in `block in run'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:133:in `standard_exception_handling'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/applica
tion.rb:63:in `run'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/bin/rake:33:in `
<top (required)>'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/bin/rake:19:in `load'
/Users/sa/.rvm/gems/ruby-1.9.2-p318/bin/rake:19:in `<main>'
Tasks: TOP => db:migrate => environment

2 个答案:

答案 0 :(得分:4)

这不是您问题的直接答案,但我会将其作为答案发布,因为我认为它会对您有所帮助。

您应该使用应用程序的schema.rb文件,而不是从一开始就运行所有代码的迁移。请注意,这仅适用于您首次设置已开始处理的现有应用程序(因为它强制创建所有表,因此删除任何内容),但这是在此处加载数据库的正确方法案件。迁移用于增量更改。

将架构加载到数据库中:

rake db:schema:load

然后,您的数据库将根据schema.rb文件进行设置(Rails会出于此目的自动更新)。

修改 您的问题的原因可能是您的应用程序中的某些代码在引用roles表(或更可能是Role模型)的迁移之前被加载。当routes.rb文件中存在数据库查询时,这种情况经常发生,但它可能来自调用environment rake任务时加载的任意数量的文件({{{ 1}}任务)。

编辑2 查看堆栈跟踪:

  

/用户/ SA /文档/ AptanaWorkspace / recprototype /配置/ initializ   ers / constants.rb:1:在''

我猜:migrate文件的内容类似于config/initializers/constants.rb。每当加载Rails环境时都会加载初始化程序(Rails提供的大多数rake任务就是这种情况)。

你仍然应该使用Role.all任务,但在运行之前你需要注释掉那一行(当然,如果我的理论是正确的话)。

答案 1 :(得分:4)

似乎config/initializers/constants.rb正在对要求角色表存在的Role类做一些事情。当您运行迁移时,初始化程序会正常加载,因此您的应用程序会崩溃,因为该表尚不存在。

在角色表在其计算机上创建之后,在角色表上添加此依赖关系的开发人员可能会这样做。

你可以

  • 尝试加载架构而不是运行迁移rake db:schema:load,假设schema.rb已签入。如果加载架构加载初始化器,这将无效 - 我不记得是否是这种情况
  • 从SQL转储恢复数据库
  • 注释掉有问题的代码(或者使用表存在的测试对其进行括号),并运行迁移/加载模式 - 执行任何操作对于运行迁移都是不可能的。