user984621
user984621

Reputation: 48453

Rails - MySQL errors "Out of range value" even though the range should be ok?

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.

What am I overlooking?

Upvotes: 1

Views: 1063

Answers (2)

Rick James
Rick James

Reputation: 142298

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

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

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

Upvotes: 1

tadman
tadman

Reputation: 211590

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.

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.

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.

Upvotes: 4

Related Questions