I can't seem to figure this one out by searching Google.
我似乎无法通过搜索谷歌找到这个。
I have an existing database which has some columns with the type of double
, what would I write in my rails migration to create an equivalent column type? It's a relatively old database and there maybe a more suitable datatype to use completely, but I wanted to see what others thought.
我有一个现有的数据库,它有一些列类型为double,我要在rails迁移中编写什么来创建一个等效的列类型?这是一个相对古老的数据库,可能有一个更适合完全使用的数据类型,但是我想看看其他人是怎么想的。
I was going to use the type of :decimal
, is this the best choice?
我要用的类型是:decimal,这是最好的选择吗?
Thoughts?
想法吗?
5 个解决方案
#1
10
There's no specific type for double, Rails tries to be smart about it:
对于double没有特定的类型,Rails试图聪明地做到这一点:
The numeric (integer/decimal/float) column definitions use the :limit
option for the number of bytes in an integer column type, and the :scale
/:precision
options for floats.
数字(整数/十进制/浮点)列定义使用:limit选项表示整数列类型中的字节数,以及浮点数的:scale/:precision选项。
(precision is the number of significant digits; scale is how many of these fall after the decimal point.)
(精度是有效位数的个数;标度是这些数字中有多少落在小数点后)
#2
20
In my case (for test-db preparation):
在我的情况下(用于测试-db准备):
MySQL (with mysql2(0.3.11) driver):
MySQL(mysql2(0.3.11)驱动程序):
double(64,12)
Rails (in db/schema.rb):
Rails(db / schema.rb):
t.float :limit=>64 ==> failed
t.float :limit=>53 ==> occasionally succeeded
t.decimal :precision=>64, :scale=>12 ==> fully succeeded
#3
8
You can get Rails to create a DOUBLE
. I'm not sure how "officially" supported the following solution is, but it works for MySQL. Just set the limit to 53. e.g.:
您可以让Rails创建一个DOUBLE。我不确定如何“正式”支持以下解决方案,但它适用于MySQL。把上限设为53。例如:
t.float :published_at, :limit => 53, :null => true
t。float:published_at,:limit => 53,:null => true
I got the answer from this blog post and there is some more testing results regarding this there.
我从这篇博文中得到了答案,这里有更多的测试结果。
#4
4
I'm seeing a problem with this solution. If I have a declaration like this:
我发现这个解决方案有问题。如果我有这样的声明:
t.float :latitude, :limit => 30
everything looks great: the database schema shows the column is declared as a double. The problem is that schema.rb lacks any :limit value, so when the schema is cloned into the test environment, the column becomes a float rather than a double. Unit tests fail because there's not enough accuracy in the column.
一切看起来都很好:数据库模式显示列被声明为double。问题在于模式。rb缺少any:limit值,因此当将模式克隆到测试环境中时,列将变成浮点数而不是双精度数。单元测试失败是因为列中没有足够的准确性。
If you look at the docs for ActiveRecord column definitions what you find is this:
如果你查看ActiveRecord列定义的文档,你会发现:
:limit - Requests a maximum column length. This is number of characters for :string and :text columns and number of bytes for :binary and :integer columns.
:限制-请求最大列长度。这是:string和:text列的字符数,以及:binary和:integer列的字节数。
It's silent on the topic of floats. It appears from the code that it's a side effect of how :limit is supported that this solution works. It looks like either there's a bug in the generation of schema.rb or else it was never intended to be used this way.
在花车的话题上它是沉默的。从代码中可以看出,这是如何:该解决方案工作时所支持的limit的副作用。看起来模式生成中有一个错误。rb或者其他的,它从来没有打算用这种方式。
I'm afraid the answer may be that the :decimal data type (which I don't care for) is the fully-supported solution.
我担心答案可能是:decimal数据类型(我不喜欢)是完全支持的解决方案。
#5
1
In modern versions of Rails (I'm using 5.1)
在现代版本的Rails中(我使用5.1)
t.column :field, "double"
#1
10
There's no specific type for double, Rails tries to be smart about it:
对于double没有特定的类型,Rails试图聪明地做到这一点:
The numeric (integer/decimal/float) column definitions use the :limit
option for the number of bytes in an integer column type, and the :scale
/:precision
options for floats.
数字(整数/十进制/浮点)列定义使用:limit选项表示整数列类型中的字节数,以及浮点数的:scale/:precision选项。
(precision is the number of significant digits; scale is how many of these fall after the decimal point.)
(精度是有效位数的个数;标度是这些数字中有多少落在小数点后)
#2
20
In my case (for test-db preparation):
在我的情况下(用于测试-db准备):
MySQL (with mysql2(0.3.11) driver):
MySQL(mysql2(0.3.11)驱动程序):
double(64,12)
Rails (in db/schema.rb):
Rails(db / schema.rb):
t.float :limit=>64 ==> failed
t.float :limit=>53 ==> occasionally succeeded
t.decimal :precision=>64, :scale=>12 ==> fully succeeded
#3
8
You can get Rails to create a DOUBLE
. I'm not sure how "officially" supported the following solution is, but it works for MySQL. Just set the limit to 53. e.g.:
您可以让Rails创建一个DOUBLE。我不确定如何“正式”支持以下解决方案,但它适用于MySQL。把上限设为53。例如:
t.float :published_at, :limit => 53, :null => true
t。float:published_at,:limit => 53,:null => true
I got the answer from this blog post and there is some more testing results regarding this there.
我从这篇博文中得到了答案,这里有更多的测试结果。
#4
4
I'm seeing a problem with this solution. If I have a declaration like this:
我发现这个解决方案有问题。如果我有这样的声明:
t.float :latitude, :limit => 30
everything looks great: the database schema shows the column is declared as a double. The problem is that schema.rb lacks any :limit value, so when the schema is cloned into the test environment, the column becomes a float rather than a double. Unit tests fail because there's not enough accuracy in the column.
一切看起来都很好:数据库模式显示列被声明为double。问题在于模式。rb缺少any:limit值,因此当将模式克隆到测试环境中时,列将变成浮点数而不是双精度数。单元测试失败是因为列中没有足够的准确性。
If you look at the docs for ActiveRecord column definitions what you find is this:
如果你查看ActiveRecord列定义的文档,你会发现:
:limit - Requests a maximum column length. This is number of characters for :string and :text columns and number of bytes for :binary and :integer columns.
:限制-请求最大列长度。这是:string和:text列的字符数,以及:binary和:integer列的字节数。
It's silent on the topic of floats. It appears from the code that it's a side effect of how :limit is supported that this solution works. It looks like either there's a bug in the generation of schema.rb or else it was never intended to be used this way.
在花车的话题上它是沉默的。从代码中可以看出,这是如何:该解决方案工作时所支持的limit的副作用。看起来模式生成中有一个错误。rb或者其他的,它从来没有打算用这种方式。
I'm afraid the answer may be that the :decimal data type (which I don't care for) is the fully-supported solution.
我担心答案可能是:decimal数据类型(我不喜欢)是完全支持的解决方案。
#5
1
In modern versions of Rails (I'm using 5.1)
在现代版本的Rails中(我使用5.1)
t.column :field, "double"