2011-12-02 61 views
3

編輯來自normalocity:我的答案還沒有能夠解決這個問題,所以我發佈了一個替代解決方案的賞金。rake db:schema:load找不到表格


原題:

我開始一個星期前在Rails項目工作,不斷變化的多次使用的遷移數據庫模式。昨天我嘗試使用這些命令從頭開始創建我的數據庫(研究與開發環境在默認配置):

rake db:drop 
rake db:create 
rake db:schema:load 

使用--trace選項裝載模式,當我得到這個消息:

** Invoke db:schema:load (first_time) 
** Invoke environment (first_time) 
** Execute environment 
rake aborted! 
Could not find table 'galleries' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:439:in `table_structure' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:322:in `columns' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `block (2 levels) in initialize' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in `with_connection' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:92:in `block in initialize' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:106:in `yield' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:106:in `default' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:106:in `block in initialize' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:717:in `yield' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:717:in `default' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:717:in `columns_hash' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/locking/optimistic.rb:145:in `locking_enabled?' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/relation.rb:110:in `to_a' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/relation/finder_methods.rb:159:in `all' 
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:441:in `all' 
/home/fabien/Dropbox/portfolio/app/admin/photos.rb:10:in `block in <top (required)>' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/dsl.rb:13:in `instance_eval' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/dsl.rb:13:in `run_registration_block' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/namespace.rb:171:in `parse_registration_block' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/namespace.rb:50:in `register' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/application.rb:87:in `register' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin.rb:60:in `register' 
/home/fabien/Dropbox/portfolio/app/admin/photos.rb:1:in `<top (required)>' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:234:in `load' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:234:in `block in load' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `block in load_dependency' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:234:in `load' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/application.rb:132:in `block in load!' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/application.rb:132:in `each' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/application.rb:132:in `load!' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/application.rb:155:in `routes' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin.rb:63:in `routes' 
/home/fabien/Dropbox/portfolio/config/routes.rb:9:in `block in <top (required)>' 
/var/lib/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/routing/route_set.rb:258:in `instance_exec' 
/var/lib/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/routing/route_set.rb:258:in `eval_block' 
/var/lib/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/routing/route_set.rb:235:in `draw' 
/home/fabien/Dropbox/portfolio/config/routes.rb:1:in `<top (required)>' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:234:in `load' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:234:in `block in load' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `block in load_dependency' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:234:in `load' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/application/routes_reloader.rb:29:in `block in load_paths' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/application/routes_reloader.rb:29:in `each' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/application/routes_reloader.rb:29:in `load_paths' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/application/routes_reloader.rb:13:in `reload!' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:87:in `reload_routes!' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/reloader.rb:34:in `reload!' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/reloader.rb:28:in `block in initialize' 
/ var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/file_update_checker.rb:32:in `call' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/file_update_checker.rb:32:in `execute_if_updated' 
/var/lib/gems/1.9.1/gems/activeadmin-0.3.4/lib/active_admin/reloader.rb:45:in `block in attach!' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:404:in `_run_prepare_callbacks' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks' 
/var/lib/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/reloader.rb:46:in `prepare!' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/application/finisher.rb:41:in `block in <module:Finisher>' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `instance_exec' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `run' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:55:in `block in run_initializers' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `each' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `run_initializers' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:96:in `initialize!' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/railtie/configurable.rb:30:in `method_missing' 
/home/fabien/Dropbox/portfolio/config/environment.rb:5:in `<top (required)>' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `block in require' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `block in load_dependency' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency' 
/var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:83:in `require_environment!' 
/var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:193:in `block (2 levels) in initialize_tasks' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain' 
/usr/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:176:in `block in invoke_prerequisites' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `each' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `invoke_prerequisites' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:157:in `block in invoke_with_call_chain' 
/usr/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run' 
/var/lib/gems/1.9.1/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>' 
/usr/local/bin/rake:19:in `load' 
/usr/local/bin/rake:19:in `<main>' 
Tasks: TOP => db:schema:load => environment 

鐵軌初學者,我不知道如何解決這個問題,尤其是我的schema.rb有創建畫廊表的命令。

我的代碼可以在這裏找到:https://github.com/fabienengels/portfolio

+1

代碼中最頂級的代碼行是'/home/fabien/Dropbox/portfolio/app/admin/photos.rb:10:in'block in ''你在哪裏參考'Gallery.all.each' - 你是否對該文件/行進行了最近的更改?檢查你的'git log'。另外,在'db:drop'之後,你是否嘗試了'db:migrate'而不是'db:schema:load'來查看是否有區別?它可能會因特定的遷移而失敗,也許這有助於追蹤問題。 – jefflunt

+0

評論app/admin/photos.rb: Gallery.all.each do | gallery | scope gallery.slug.to_sym do | photos | photos.where(:gallery_id => gallery) 結束 結束 它的工作原理...我怎樣才能解決這個問題沒有每次都評論這些行我想創建數據庫? (謝謝你的線索!:)) –

回答

2

您需要修改/app/admin/photos.rb

# /app/admin/photos.rb 
index :as => :grid, :columns => 6 do |photo| 
    Gallery.all.each do |gallery| 
     scope gallery.slug.to_sym do |photos| 
     photos.where(:gallery_id => gallery) 
     end 
    end 
    # link_to(image_tag(photo.file.url(:thumb)), admin_photo_path(photo)) 
end 

,你需要把這些代碼的index塊內。我在那裏註釋掉了你的鏈接標籤。我會留給你想出如何設計你的網格/塊/什麼。只要確保它在索引範圍內即可。祝你好運。

+0

它的作品...很明顯,如此簡單,我跌倒了愚蠢的:)謝謝! :) –

+0

祝賀賞金。 – jefflunt

0

我以前從未使用過ActiveAdmin,但是從文檔,但似乎你只能在一個ActiveAdmin.register塊內引用單個模型,或者你根本避風港」 t使Gallery模型在該文件內部可見。

因此,在app/admin/photos.rb,你必須

ActiveAdmin.register Photo do 
    ... 

    Gallery.all.each do |gallery|    # <= this is invalid, because this file 
    scope gallery.slug.to_sym do |photos| # doesn't know what `Gallery` (and therefore 
     photos.where(:gallery_id => gallery) # `galleries` table`) is referring to 
    end 
    end 

    .... 
end 

我不知道,即使你可怎麼辦嵌套ActiveAdmin註冊,或者,但你得到的錯誤消息告訴你,Gallery是無法在此文件範圍內訪問,因此,它就像是「什麼」畫廊表?我們在這裏沒有「畫廊」表。「

所以,你可以嘗試這兩件事情之一來解決它,看看會發生什麼 - 希望這是任何答案,或將導致你的答案:

  • app/admin/photos.rb的最頂端文件中添加這個確切的行:
    • require '../models/gallery'
  • 如果不工作,然後代替Gallery.all.each...嘗試
    • ActiveAdmin.register Gallery do其次是同一塊
+0

我增加了需要對線路,但現在我得到這個錯誤: 小煜@ mrtea:〜/ Dropbox的/組合$耙分貝:模式:負載 耙中止! 沒有這樣的文件加載 - ../models/gallery 我不認爲你的第二個解決方案將與ActiveAdmin一起工作,它會顯示在管理界面的錯誤部分的鏈接。 –

+0

好吧,我只是有路徑錯誤。基本上你想要做的是'require'/ path/to/app/models/gallery.rb''。失敗只是告訴你我給你的道路是不正確的。但是,如果糾正路徑不起作用,那麼我不知道如何解決它,並會撤回我的答案。 – jefflunt

+0

我嘗試過:require'app/models/gallery.rb' 我得到了: fabien @ mrtea:〜/ Dropbox/portfolio $ rake db:schema:load rake aborted! 沒有這樣的文件加載 - app/models/gallery.rb 嘗試:require'gallery.rb'也不工作(我得到了最初的錯誤:/) –

2

所以問題是,當你的應用程序加載時,它加載了你的activeadmin的東西。這反過來結束運行Gallery.all知道什麼範圍設置。

運行類似schema:load的耙子任務仍然會加載應用程序,所以它仍然在嘗試執行Gallery.all,但是您剛剛刪除/重新創建了數據庫,因此完全可以說該表格不在那裏。

你希望能夠做的是告訴ActiveAdmin不要打擾自己配置,因爲你只是在運行一些遷移。

幸運的是,在這些情況下設置了全局變量$rails_rake_task。如果$rails_rake_task爲true,或者如果這不起作用,並且沒有執行任何activeadmin配置(例如app/admin/photos中的內容),則$rails_rake_task爲true,則嘗試不在active_admin初始程序中運行代碼。