2

I just inherited code that I'm attempting to run the migrations for but I keep getting a rake aborted error. I've come across others that have what appears to be similar issues, but most involved Heroku and I'm trying to run this locally (to start.)

I've tried troubleshooting using both PostgreSQL and SQLite, and both produce the same issue. The table "roles" referenced is the second migration called, so I'm having a hard time figuring out what is causing it to not get built. Any and all assistance is greatly appreciated. Thanks in advance.

Here's the roles migration:

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

      t.timestamps
    end
  end
end

Here is the trace for 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

Here is the trace for 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モデル) を参照する移行前にアプリケーションのコードがロードされることです。これは、ファイルにデータベースクエリがある場合に頻繁に発生しますが、 rakeタスクが呼び出さroutes.rbれたときに読み込まれる任意の数のファイルから発生する可能性があります(タスクの場合)。environment:migrate

編集2スタックトレースを確認します。

/Users/sa/Documents/AptanaWorkspace/recprototype/config/initializers/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 に答える