ruby 尝试保存大(ish)整数值时,出现指示数字的错误“超出了 ActiveRecord::Type::Integer 的范围,限制为 4”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29538530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting error indicating number is "out of range for ActiveRecord::Type::Integer with limit 4" when attempting to save large(ish) integer value
提问by Sergey
I'm using SQLite + ActiveRecord in my Ruby app, and here's the error I get while trying to write a big number to the integer field:
我在我的 Ruby 应用程序中使用 SQLite + ActiveRecord,这是我在尝试将一个大数字写入整数字段时遇到的错误:
1428584647765 is out of range for ActiveRecord::Type::Integer with limit 4
1428584647765 超出了 ActiveRecord::Type::Integer 的范围,限制为 4
But according to the SQLite documentation:
但根据 SQLite 文档:
The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
该值是一个有符号整数,根据值的大小存储在 1、2、3、4、6 或 8 个字节中。
8 bytes is a plenty of space to store the integer 1428584647765, so why does ActiveRecord give me an error? Why does it think that this is a 4-byte field?
8 个字节足以存储整数 1428584647765,那么为什么 ActiveRecord 会报错呢?为什么它认为这是一个 4 字节的字段?
回答by Afolabi Olaoluwa Akinwumi
I ran into the same problem, and the answer above gave me a clue on how I fixed mine. I propose my answer a little detailed to solving the problem.
我遇到了同样的问题,上面的答案给了我一个关于如何修复我的问题的线索。我提出我的答案有点详细,以解决问题。
You can do this by setting limit on your table column.
您可以通过在表格列上设置限制来做到这一点。
Hack/Steps
黑客/步骤
Run a migration to change your table column. e.g.
rails generate migration change_integer_limit_in_your_tableNote:
your_tablein the code will be your table name in pluralEdit the generated migration such that the generated migration will look like this:
class ChangeIntegerLimitInYourTable < ActiveRecord::Migration def change change_column :your_table, :your_column, :integer, limit: 8 end endNote: The limit of 8 in the code is the storage size which can range from -9223372036854775808 to +9223372036854775807 and called
biginti.e. large-range integer.Run
rake db:migrateto migrate your database.Restart your server by running
rails serverand you are up and running.
运行迁移以更改表列。例如
rails generate migration change_integer_limit_in_your_table注意:
your_table在代码中将是您的表名的复数形式编辑生成的迁移,使生成的迁移如下所示:
class ChangeIntegerLimitInYourTable < ActiveRecord::Migration def change change_column :your_table, :your_column, :integer, limit: 8 end end注意:代码中8的限制是存储大小,范围从-9223372036854775808到+9223372036854775807,称为
bigint大范围整数。运行
rake db:migrate以迁移您的数据库。通过运行重新启动您的服务器
rails server,您就可以启动并运行了。
For more information on Numeric Type, see https://www.postgresql.org/docs/9.4/static/datatype-numeric.html
有关数字类型的更多信息,请参阅https://www.postgresql.org/docs/9.4/static/datatype-numeric.html
回答by diderevyagin
Good day. By default columnt create with len = 32 bytes
再会。默认 columnt 使用 len = 32 字节创建
for change this, you can create migration, for example:
为了改变这一点,你可以创建迁移,例如:
t.integer :some_field, :limit => 8
回答by Brunno Gomes
I stumbled upon this problem today with PostgreSQL too. I know the question is old and the author was using SQLite but since a lot of people seem to end up here like I did let me post an updated solution.
我今天也用 PostgreSQL 偶然发现了这个问题。我知道这个问题很老,作者使用的是 SQLite,但因为很多人似乎都在这里结束,就像我让我发布了一个更新的解决方案一样。
For Rails 5.1(I believe for 4.1 and above but didn't test) and PostgreSQL(probably with other databases too) the correct way is to set the column type to bigint, using the limit: 8option doesn't affect the database.
对于Rails 5.1(我相信 4.1 及更高版本,但没有测试)和PostgreSQL(可能也有其他数据库),正确的方法是将列类型设置为bigint,使用该limit: 8选项不会影响数据库。
This is the example code using a table named products and a column named quantity:
这是使用名为 products 的表和名为数量的列的示例代码:
class ChangeProductsQuantityToBigint < ActiveRecord::Migration[5.1]
def change
change_column :products, :quantity, :bigint
end
end

