2

我只是继承了我试图为其运行迁移的代码,但我不断收到 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
4

2 回答 2

4

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

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

要将架构加载到数据库中:

rake db:schema:load

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

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

编辑 2查看您的堆栈跟踪:

/Users/sa/Documents/AptanaWorkspace/recprototype/config/initializ ers/constants.rb:1:in `'

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

您仍然应该使用该rake db:schema:load任务,但是您需要在运行它之前注释掉该行(当然,如果我的理论是正确的)。

于 2012-10-03T03:29:13.690 回答
4

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

很可能在角色表在他们的机器上创建之后,添加对现有角色表的依赖的开发人员这样做了。

你可以

  • 假设 schema.rb 已签入,尝试加载架构而不是运行迁移rake db:schema:load。如果加载架构加载初始化程序,这将不起作用 - 我不记得是否是这种情况
  • 从 SQL 转储中恢复数据库
  • 注释掉有问题的代码(或用表存在的测试将其括起来),然后运行迁移/加载模式 - 所做的任何事情都不太可能对运行迁移至关重要。如果注释掉该代码会产生更多故障,请使用源代码管理将应用程序恢复到最初签入迁移后的状态,希望任何此类问题都更容易解决
于 2012-10-03T03:40:10.187 回答