我的应用程序中有一个自动递增的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>
它的限制为8,范围远远超过了我试图保存的值,但我仍然得到了相同的错误:
2.2.2 :045 > MyModelName.new.save
RangeError: 2147483657 is out of range for ActiveRecord::Type::Integer with limit 4
值得注意的是,autoincrement值正在更新,即使记录没有保存。如果我再试一次,ID值会递增:
RangeError: 2147483658 is out of range for ActiveRecord::Type::Integer with limit 4
版本信息
activerecord (4.2.5.1)
mysql2 (0.3.18)
是否有任何update/after_save回调将此值推荐给同样需要调整大小的类似外键?