开发者

Rails 3 Migration Alter Float Length/Decimal

开发者 https://www.devze.com 2023-03-14 02:58 出处:网络
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:

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消