Ruby MySQL 字段名为 type 引起的错误

hxh1246996371 · 2015年09月14日 · 最后由 est 回复于 2015年09月15日 · 5891 次阅读

继我上次的问题 https://ruby-china.org/topics/27309

当在别人的数据库表里面有 type 字段的时候就报了下面的错,有点不解:

The single-table inheritance mechanism failed to locate the subclass: '0'. 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 ApmSellMem.inheritance_column to use another column for that information.

已经告诉你type是保留字,要重命名你的字段名

这些都是 rails 里的保留字段

图片引用自 Rails 实战圣经

3 楼 已删除
class Example < ActiveRecord::Base
  self.inheritance_column = "_type"
end

#5 楼 @Ddl1st 真行呢,不过有副作用吗

没有,如果需要 STI 的话,添加一个 _type 字段到数据库

可以看一下这篇文章,单表继承

除了 ActiveRecord 的保留字,有时和 Ruby 的方法重名也会有问题。原来遇到过字段名是 format,结果貌似和 Kernel#format 方法冲突了。

哈哈,我以前写 django 的,有一次自己定义了个方法名字叫 save()。然后整个项目都不好了。老出错。。。。。。。。。。。。。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号