Rails - MySQL错误“超出范围值”,即使范围应该是ok的?

时间:2022-02-15 16:43:50

This is my column:

这是我的专栏:

t.decimal  "update_timezone_offset",     precision: 10, scale: 6

This is the value I am trying to save:

这是我试图保存的值:

-14400

And I get this error:

我得到了这个错误

Mysql2::Error: Out of range value for column 'update_timezone_offset' at row 1:

I also tried variable_with_the_amount.to_f, but the result was the same.

我也试过variable_with_the_amount。to_f,但是结果是一样的。

What am I overlooking?

我俯瞰着什么呢?

2 个解决方案

#1


3  

A DECIMAL(10,6) allows for 4 places before the decimal, 6 after, for a total of 10, but 14,400 requires 5. You'll need DECIMAL(11,6) or DECIMAL(10,5) to handle that value.

十进制(10,6)允许小数点前4位,小数点后6位,总共10位,但14,400位需要5位。您需要十进制(11、6)或十进制(10、5)来处理该值。

If you're dealing with time-zone offsets, DECIMAL(10,2) should be more than sufficient if this is in hours. If in seconds you don't need any decimal places at all.

如果你在处理时区偏移,十进制(10,2)应该足够,如果这是小时。如果以秒为单位,则不需要小数点。

Additionally, remember that time-zone offsets change wildly over the course of the year and are not fixed things, and in many cases are not even predictable. The whims of politicians can change them at any time.

此外,请记住,时区偏移在一年的过程中发生了很大的变化,并不是固定的事情,在许多情况下甚至是无法预测的。政治家们的心血来潮可以随时改变他们。

#2


1  

DECIMAL(10,6) means 10 digits, including 6 after the decimal point. That is, +/-9999.999999 is the limit.

DECIMAL(10,6)表示10位数字,包括小数点后的6位。那就是,+/-9999.999999是极限。

If that is supposed to be minutes, then you have 10 days; is that what you wanted?

如果这是分钟,那么你有10天;这就是你想要的吗?

For seconds, 86400 is one day, so DECIMAL(11,6) might be what you wanted.

几秒钟,86400是一天,所以十进制(11,6)可能是你想要的。

#1


3  

A DECIMAL(10,6) allows for 4 places before the decimal, 6 after, for a total of 10, but 14,400 requires 5. You'll need DECIMAL(11,6) or DECIMAL(10,5) to handle that value.

十进制(10,6)允许小数点前4位,小数点后6位,总共10位,但14,400位需要5位。您需要十进制(11、6)或十进制(10、5)来处理该值。

If you're dealing with time-zone offsets, DECIMAL(10,2) should be more than sufficient if this is in hours. If in seconds you don't need any decimal places at all.

如果你在处理时区偏移,十进制(10,2)应该足够,如果这是小时。如果以秒为单位,则不需要小数点。

Additionally, remember that time-zone offsets change wildly over the course of the year and are not fixed things, and in many cases are not even predictable. The whims of politicians can change them at any time.

此外,请记住,时区偏移在一年的过程中发生了很大的变化,并不是固定的事情,在许多情况下甚至是无法预测的。政治家们的心血来潮可以随时改变他们。

#2


1  

DECIMAL(10,6) means 10 digits, including 6 after the decimal point. That is, +/-9999.999999 is the limit.

DECIMAL(10,6)表示10位数字,包括小数点后的6位。那就是,+/-9999.999999是极限。

If that is supposed to be minutes, then you have 10 days; is that what you wanted?

如果这是分钟,那么你有10天;这就是你想要的吗?

For seconds, 86400 is one day, so DECIMAL(11,6) might be what you wanted.

几秒钟,86400是一天,所以十进制(11,6)可能是你想要的。