0

I need to change a couple of fields in my database from:

:decimal, :precision => 8, :scale => 5

to:

:float

Does this migation result in data loss? The current data consists of integers between 0 and 999.

If this migration will impact these numbers already stored, how can I keep this data safe?

Setup: Ruby on Rails 3 running on Heroku (solution would need to work for both PostgreSQL and MySQL).

sscirrus
  • 55,407
  • 41
  • 135
  • 228

2 Answers2

1

It will, if you ever need to do exact comparisons or floating point arithmetics on your numbers. Open up PostgreSQL and try this:

select floor(.8::float * 10.0::float); -- 8
select floor((.1::float + .7::float) * 10.0::float); -- 7

See this related question for the reason:

Why do simple doubles like 1.82 end up being 1.819999999645634565360?

Community
  • 1
  • 1
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
  • thanks for your answer - I'm more concerned with literally losing my numbers than having them slightly off their former value. If the field goes from that restrictive decimal to either float or a more expansive decimal (higher precision), I assume this means my data would remain safe? – sscirrus May 15 '11 at 05:08
  • Well, in this case yes, you're safe if you use an `ALTER TABLE` statement to modify the column's type. RoR might get the job done too, I've honestly no idea -- though if so, I'd place more trust in what I type than in a ruby-based SQL diff tool. – Denis de Bernardy May 15 '11 at 05:14
0

Integers between 0 and 999 will fit in either and the data won't be impacted. If it is just integers - why not use ints?

zsalzbank
  • 9,685
  • 1
  • 26
  • 39