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

作者在 2018-04-01 21:40:53 发布以下内容

使用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

Ruby | 阅读 3354 次
文章评论,共0条
游客请输入验证码
浏览2883821次
文章归档
最新评论
  • 时光拾荒者:CtrlCV还是强的😝
  • 硬识岩丝:解决了,太感谢了~
  • xiwang12:路过
  • 里苦不功:不校验mysql版本是否与当前django框架是否兼容