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, :l开发者_Python百科ongitude, :float, {:length => 0, :decimals => 0}
end
end
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
?
精彩评论