0

I am renaming one column ("old_name" to "new_name") and converting other column ("column2") from integer to boolean in a Rails migration:

def up
  rename_column :my_models, :old_name, :new_name unless column_exists?(:my_models, :new_name)
  ...........
  if column_exists?(:my_models, :column2) && !column_exists?(:my_models, :column2_num) && MyModel.columns_hash['column2'].type==:integer
    rename_column :my_models, :column2, :column2_num
    add_column :my_models, :column2, :boolean, :null => false, :default => true

    MyModel.reset_column_information

    MyModel.all.each do |item|
      item.column2 = (item.column2_num!=0)
      item.save!
    end
    remove_column :my_models, :column2_num
  end
  ...........
end

rake db:migrate results in:

undefined method `old_name' for #<MyModel:0x4ff4238>

Error occurs in "save!" method.

I tried also:

MyModel.connection.schema_cache.clear!
MyModel.reset_column_information

id does not work too.

It is worth saying that I have applied previous version of this migration once and after rollback ("down" method is empty) am trying to apply it again. So, the database schema currently does not contain "old_name" because it was not reverted physically.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Paul
  • 25,812
  • 38
  • 124
  • 247

1 Answers1

1

I had the same problem recently and I found that it was not Rails causing the problem but an update trigger in the database that was still referring to the old column name.

Removed that part of the trigger and the migration went through.

josht
  • 61
  • 8