gpt4 book ai didi

ruby-on-rails - 没有迁移表的 Rails STI 模型

转载 作者:行者123 更新时间:2023-12-04 07:35:47 25 4
gpt4 key购买 nike

我有一个模型,比如 User,还有一个模型 Admin,它继承自 User。我在 lib 文件夹中有一些代码使用了这两个模型。但是,如果我安装一个新的数据库并尝试运行迁移,rails 会崩溃,提示没有 User 这样的模型。是否有绕过此错误的最佳做法?

`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/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_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/ruby-1.9.3-p362-railsexpress@test_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/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:426:in `columns'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:228:in `yield'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:228:in `columns'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:237:in `columns_hash'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/inheritance.rb:19:in `descends_from_active_record?'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/inheritance.rb:25:in `finder_needs_type_condition?'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/base.rb:455:in `relation'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_interface_install/gems/activerecord-3.2.11/lib/active_record/scoping/named.rb:37:in `scoped'
/home/arthurborisow/.rvm/gems/ruby-1.9.3-p362-railsexpress@test_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)>'`

最佳答案

当设置一个全新的数据库(而不是升级现有数据库)时,最好使用 rake db:setup 加载 schema.rb 文件连续运行每个迁移。这个问题很好地解释了各种 db Rake 任务:

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

关于ruby-on-rails - 没有迁移表的 Rails STI 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15757276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com