rails不能在model中使用type作为字段名的解决办法

Ruby | 2018-04-01 21:40:53 | 442次阅读 | 0评

使用type作为字段名会出现这个错误:

ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'xxxx'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite SmsRecord.inheritance_column to use another column for that information.


解决办法:

在相关model中加入“self.inheritance_column = nil”即可,如:

class SmsRecord < ApplicationRecord
	self.inheritance_column = nil
end


-------------------------------- 作者在 2018-04-01 21:46:38 补充以下内容 --------------------------------

相关参考:

https://stackoverflow.com/questions/11984893/issue-with-column-name-type-in-rails-3

https://stackoverflow.com/questions/7134559/rails-use-type-column-without-sti

https://apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/inheritance_column

博友评论,共0条
浏览1601486次
最新评论