微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – Rails 5 – PG :: UndefinedTable:错误:关系“application_records”不存在

我正在尝试使用 postgresql设置一个新的Rails 5应用程序( ruby 2.3.1,rails 5.0.0.rc1),设计gems并且由于以下错误而无法运行rails db:seed:

PG::UndefinedTable: ERROR:  relation "application_records" does not exist
LINE 8:                WHERE a.attrelid = '"application_records"'::r...
                                          ^
/Users//.rvm/gems/ruby-2.3[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql/database_statements.rb:88:in `async_exec'
/Users/foo/.rvm/gems/ruby-2.3[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql/database_statements.rb:88:in `block in query'
/Users/foo/.rvm/gems/ruby-2.3[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
/Users/foo/.rvm/gems/ruby-2.3[email protected]/gems/activesupport-5.0.0.rc1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/foo/.rvm/gems/ruby-2.3[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
/Users/foo/.rvm/gems/ruby-2.3[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql/database_statements.rb:87:in `query'
/Users/foo/.rvm/gems/ruby-2.3[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql_adapter.rb:739:in `column_deFinitions'
/Users/foo/.rvm/gems/ruby-2.3[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:227:in `columns'

经过大量的谷歌搜索后,我意识到这与ApplicationRecord base class change in rails 5有关.显然,没有名为application_records的表,因此active_support不应该寻找它.我已经检查过app / models / application_record.rb是否存在且具有正确的内容.此外,用户模型(当前我的应用程序中唯一的模型)按预期扩展了ApplicationRecord:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable,:lockable,:timeoutable and :omniauthable
  devise :database_authenticatable,:recoverable,:rememberable,:trackable,:validatable
end

rails db:migrate运行正常,但rails db:seed chokes有上述错误.

任何人都可以了解可能导致这种情况的原因吗?

application_record.rb的内容

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

解决方法

回答我自己的问题,这可能有助于其他人遇到类似的问题.

正如@PrathameshSonpatki在rails issue上提到的那样,这是因为userstamp gem在ActiveRecord :: Base而不是ApplicationRecord上注入了关系. (请注意,在撰写本文时,userstamp gem不兼容rails 5).

如果您有一个尚未使用rails 5测试的gem,并且您遇到一个问题,其中active_support会查找名为application_records的表,请检查您使用的某个gem是否在ActiveRecord :: Base类上注入了关系.基于this blog post,ApplicationRecord的目的是避免全局注入ActiveRecord :: Base.因此,您需要查找与Rails 5兼容的此类gem的版本或修补此类gem以将必要的行为注入ApplicaitonRecord而不是ActiveRecord :: Base.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐