Question

Solved!

Thanks to all so much. It seems was a conflict of the migration versions. I just deleted last records from the scheme_migrations table related with the column renaming. Also i deleted all migrations files in db/migrations related with the renaming.

I have a table products_images:

class CreateProductsImages < ActiveRecord::Migration
  def change
    create_table :products_images, id: false do |t|
      t.references :product
      t.references :image
      t.integer :position
    end
    add_index :products_images, [:product_id, :image_id]
    add_index :products_images, [:image_id, :product_id]
  end
end

When I have tried to start the migration to rename a column position to priority rails made migration without any errors. But no changes in DB table products_images were applied. Of course I could delete the column and create new one with another name. Just a curiosity - why? DB is SQLite.
Migrations of renaming:
variant 1

class RenamePositionToPriority < ActiveRecord::Migration
  def change
    rename_column :products_images, :position, :priority
  end
end

variant 2:

class RenamePositionToPriority < ActiveRecord::Migration
  def change
    #rename_column :products_images, :position, :priority
    change_table :products_images do |t|
      t.rename :position, :priority
    end
  end
end

rake db:rollback outputs:

DL is deprecated, please use Fiddle
==  RenamePositionToPriority: reverting =======================================
-- rename_column(:products_images, :priority, :position)
rake aborted!
An error has occurred, this and all later migrations canceled:

Missing column products_images.priorityC:in `migrate'
Tasks: TOP => db:rollback
Was it helpful?

Solution

Step 1: rails g migration RenameColumn

Step 2: before rake db:migrate open migration file on /db folder

class RenameColumn < ActiveRecord::Migration

 def self.up
   rename_column :products_images, :position, :priority
 end

 def self.down
    rename_column :products_images, :priority, :position
end

end

Step 3: rake db:migrate

OTHER TIPS

class RenamePositionToPriority < ActiveRecord::Migration

def self.up rename_column :products_images , :position, :priority rename_column :products_images , :position, :priority end

def self.down rename_column :products_images , :position , :priority rename_column :products_images , :position , :priority end end

can you please try this migration

Is there any chance what you could 'change' migration code after it was run? Just to clarify couple of things: Rails has no way to 'remember' a set of changes you've made by migrations. It just creates table called schema_migrations with version column in it. Basically, this table contains a list of versions (which identify a migration).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top