2013-04-02 111 views
3

我有一個模型,說User,和一個模型Admin,它繼承了User。我在lib文件夾中有一些代碼使用這兩種模式。但是,如果我安裝一個新的數據庫,並嘗試運行migrations rails崩潰,說沒有像User這樣的模型。有沒有最佳做法來繞過這個錯誤?Rails STI模型沒有遷移表

`Mysql2::Error: Table 'testing_interface_development.users' doesn't exist: SHOW FULL FIELDS FROM `users` 
/home/arthurborisow/Programming/my_project/test_interface/lib/active_record/timed_out_queries_hotfix.rb:9:in `query' 
/home/arthurborisow/Programming/my_project/test_interface/lib/active_record/timed_out_queries_hotfix.rb:9:in `query_with_reconnect' 
/home/arthurborisow/Programming/my_project/test_interface/lib/active_record/timed_out_queries_hotfix.rb:31:in `block in execute' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log' 
/home/arthurborisow/Programming/my_project/test_interface/lib/active_record/timed_out_queries_hotfix.rb:31:in `execute' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:259:in `execute_and_free' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:426:in `columns' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:228:in `yield' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:228:in `columns' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:237:in `columns_hash' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/inheritance.rb:19:in `descends_from_active_record?' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/inheritance.rb:25:in `finder_needs_type_condition?' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/base.rb:455:in `relation' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/scoping/named.rb:37:in `scoped' 
/home/arthurborisow/.rvm/gems/[email protected]_interface_install/gems/activerecord-3.2.11/lib/active_record/querying.rb:9:in `where' 
/home/arthurborisow/Programming/my_project/test_interface/app/models/admin.rb:15:in `<class:Admin>' 
/home/arthurborisow/Programming/my_project/test_interface/app/models/admin.rb:1:in `<top (required)>'` 
+0

你可以發佈確切的錯誤,將'--trace'傳遞給'rake'嗎? –

+0

你的項目中的'lib/active_record/timed_out_queries_hotfix.rb'的內容是什麼?這似乎是錯誤起源的地方,基於後面的跟蹤 –

回答

3

當建立一個完全全新的數據庫(而不是升級現有的一個),它是更好地使用rake db:setup加載,而不是運行在每個相繼遷移schema.rb文件。這個問題說明了各種分貝 Rake任務相當不錯:

Difference between rake db:migrate db:reset and db:schema:load

+0

「db:schema:load」存在同樣的問題 –

0
rake db:setup 

是最佳的選擇也

您還可以指定遷移的版本,你要哪個做使用以下命令遷移:

rake db:migrate:up VERSION=20080906120000