更新的ID列仍然因RangeError

时间:2016-05-17 21:03:23

标签: mysql ruby-on-rails activerecord

我的应用程序中有一个自动增加的ID字段,对于mysql中的中间int,它已达到2147483647的上限。我收到以下错误:

RangeError:2147483656超出ActiveRecord :: Type :: Integer的范围,限制为4

我创建了一个迁移,使ID成为一个bigint:

change_column :my_model_names, :id, :integer, limit: 8, auto_increment: true

这似乎有效 - 这是mysql命令行中字段的描述:

+-----------------------------+---------------+------+-----+---------+----------------+
| Field                       | Type          | Null | Key | Default | Extra          |
+-----------------------------+---------------+------+-----+---------+----------------+
| id                          | bigint(20)    | NO   | PRI | NULL    | auto_increment |
+-----------------------------+---------------+------+-----+---------+----------------+

在rails控制台中,当我查询MyModelName.columns时,我可以看到该列看起来正确:

2.2.2 :045 > MyModelName.columns[0]
> #<ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::Column:0x007f98bf6d6640 @strict=true, @collation=nil, @extra="auto_increment", @name="id", @cast_type=#<ActiveRecord::Type::Integer:0x007f98bf6e7490 @precision=nil, @scale=nil, @limit=8, @range=-9223372036854775808...9223372036854775808>, @sql_type="bigint(20)", @null=false, @default=nil, @default_function=nil>

它有limit = 8且范围远远超过我想要保存的值,但我仍然得到同样的错误:

2.2.2 :045 > MyModelName.new.save
RangeError: 2147483657 is out of range for ActiveRecord::Type::Integer with limit 4

有趣的是,即使未保存记录,自动增量值也会更新。如果我再试一次,ID值会增加:

RangeError: 2147483658 is out of range for ActiveRecord::Type::Integer with limit 4

版本信息

activerecord (4.2.5.1)
mysql2 (0.3.18)

1 个答案:

答案 0 :(得分:0)

是否有任何update / after_save回调将此值传播到需要调整大小的类似外键?