rake db:schema:load找不到表

时间:2011-12-02 08:43:46

标签: ruby-on-rails database rake activeadmin

来自normalocity

编辑:我的答案无法解决此问题,因此我正在为备用解决方案发布奖金。


原始问题:

我在一周前开始研究Rails项目,使用迁移多次更改数据库模式。昨天我尝试使用这些命令从头开始创建我的数据库(developpement environnement,默认配置):

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

作为Rails初学者,我不知道如何解决这个问题,尤其是我的schema.rb有命令来创建画廊表。

我的代码可在此处找到:https://github.com/fabienengels/portfolio

3 个答案:

答案 0 :(得分: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块中。我在那里注释了你的链接标签。我会告诉你如何设计你的网格/块/无论如何。只是确保它在索引范围内。祝你好运。

答案 1 :(得分:2)

所以问题是当你的应用加载时,它会加载你的activeadmin东西。这反过来最终会运行Gallery.all以了解要设置的范围。

运行像schema:load这样的rake任务仍会加载应用程序,所以它仍然尝试执行Gallery.all但你刚刚删除/重新创建了数据库,所以它非常正确地说该表不是那里。

您希望能够做的是告诉ActiveAdmin不要打扰自己配置,因为您只是在运行一些迁移。

幸运的是,在这些情况下设置了全局变量$rails_rake_task。如果$rails_rake_task为真,或者如果不起作用也不执行任何activeadmin配置(例如app / admin / photos中的内容),我会尝试不在active_admin初始化程序中运行代码$rails_rake_task是的。

答案 2 :(得分:0)

我以前从未使用过ActiveAdmin,但是从文档开始,但似乎您只能在ActiveAdmin.register块中引用单个模型,或者您根本没有使用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
    • {{1}}后跟同一个块