ALTER TABLE lhm migration deletes existing values
require 'lhm' class RenameField1ToField2ForTable < ActiveRecord::Migration def up Lhm.change_table :table do |m| m.ddl("ALTER TABLE %s CHANGE COLUMN field1 field2 FLOAT DEFAULT NULL AFTER field3" m.name) end end def down Lhm.change_table :table do |m| m.ddl("ALTER TABLE %s CHANGE COLUMN field2 field1 FLOAT DEFAULT NULL AFTER field3" m.name) end end end
- Rails-4.0: rake db:migrate
- Field was renamed successfully.
- All existing field values are erased, why? Any ideas?
- old datatype was float(11)
- MYSQL 5.6
I would highly suggest you utilize the methods made available to you from LHM that mimic the ones from Rails (if you plan to use LHM):
Lhm.change_table(:users) do |m| m.change_column(:old_column, :float) m.rename_column(:old_column, :new_column) end
If you look at the method definitions, you will see LHM is doing a MODIFY where you are doing a CHANGE for change_column, and a CHANGE for rename_column.
Can you do a straight rails migration like so:
def change rename_column :table_name, :old_column, :new_column end
I've never used lhm so this might not be possible?
Also you might not need the "FLOAT DEFAULT NULL" in your current SQL statement if it's already specified in the DB.
Hope that helps.
rename 1st_name to temporary_name, and then rename 2nd_name to 1st_name, and then rename from temporary_name to 2nd_name