Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Migration Alter Float Length/Decimal

How would I go about changing the decimals and length attributes of a float column in my Rails 3 migration file. I have tried the following w/ no success:

class IncreaseLatitudeLongitudeFieldLengths < ActiveRecord::Migration
  def self.up

    change_column :skateparks, :latitude, :float, {:length => 15, :decimals => 12}
    change_column :skateparks, :longitude, :float, {:length => 15, :decimals => 12}

  end

  def self.down

    change_column :skateparks, :latitude, :float, {:length => 0, :decimals => 0}
    change_column :skateparks, :longitude, :float, {:length => 0, :decimals => 0}

  end
end
like image 455
Kyle Decot Avatar asked Feb 23 '23 20:02

Kyle Decot


1 Answers

Personal experience what works best (since MySQL/sqlite sometimes refuses changes to columns): Create a new column, copy the data, delete the old column, rename the new column.

# Example for latitude

add_column :skateparks, :latitude2, :decimal, :precision => 15, :scale => 12
execute "UPDATE skateparks SET latitude2 = latitude"
remove_column :skateparks, :latitude
rename_column :skateparks, :latitude2, :latitude

EDIT: On the second look :float, { :length => 15, :decimals => 12 } seems to be wrong. I assume you meant: :decimal, :precision => 15, :scale => 12?

like image 51
Marcel Jackwerth Avatar answered Feb 26 '23 21:02

Marcel Jackwerth