How to Drop Columns Using Rails Migration

How to drop columns using Rails migration


remove_column :table_name, :column_name

For instance:

remove_column :users, :hobby

would remove the hobby Column from the users table.

Rails 5: How to remove a column from a database?

To remove a column with migration:

rails g migration Remove..From.. col1:type col2:type col3:type

In your case:

rails g migration RemoveCountryFromSampleApps country:string

This will generate the following migration in Rails 5.0:

class RemoveCountryFromSampleApps < ActiveRecord::Migration[5.0]
def change
remove_column :sample_apps, :country, :string
end
end

Rails removing a column from activerecord not working

The issue here is really a sneaky capitalization error. Running:

rails generate migration RemoveContainerfromCreateTasks container:references

Will generate a migration with an empty change block which will do absolutely nothing when you migrate it except modify the migrations meta table (a table that AR uses to keep track of which migrations have been run). But if you properly capitalize From:

rails generate migration RemoveContainerFromCreateTasks container:references

It will generate:

class RemoveContainerFromCreateTasks < ActiveRecord::Migration[6.0]
def change
remove_reference :create_tasks, :container, null: false, foreign_key: true
end
end

Rails isn't actually intelligent. It just casts the name argument into snake case and compares it to a set of patterns like:

remove_something_from_tablename foo:string bar:integer
create_tablename foo:string bar:integer
create_foo_bar_join_table foo bar

And it then uses a template to generate the according type of migration. If you don't properly pluralize it will be cast into:

remove_containerfrom_create_tasks

Which Rails does not know what to do with as it does not match a known pattern.

Also note despite popular belief migrations are just a DSL to create SQL transformations which is completely unaware about your tables or models. In this case the resulting migration will just blow up when you attempt to run it since you don't have a create_tasks table.

I would roll the missnamed migration back. Delete it then run:

rails g migration RemoveContainerFromTasks container:references
rails db:migrate

Removing multiple columns in table within a database. (rails)

Have you tried:

def change
remove_column :customers, :fax
remove_column :customers, :phone
end

In case you are using rails version lower than 3.x

def self.up
remove_column :customers, :fax
remove_column :customers, :phone
end

def self.down
# do something on rollback here or just do nothing
end

How to remove a column from my Rails model?

To remove a database column, you have to generate a migration:

script/rails g migration RemoveColumns

Then in the self.up class method, remove your columns:

def self.up
remove_column :table_name, :column_name
end

You may want to add them back in the self.down class method as well:

def self.down
add_column :table_name, :column_name, :type
end

The Rails Guide for this goes into much more detail.

Ruby on rails How to remove added columns and insert new columns through a migration file

You should add your remove / add column in a separate migration file.

class FooMigration < ActiveRecord::Migration
def down
remove_column :account_number, :transaction_reference_number, :information_to_account_owner
end

def up
add_column :mt940_batches, :created_by, :updated_by, :integer
end
end

Please note that your up and down method should be idem potent. You should be able to go from one to the other when calling rake db:migrate:down and rake db:migrate:up. This is not the case here.

However here, it seems that you want to achieve 2 different things in a single migration. If you want to add AND remove columns, consider moving each one in a different migration file:

Please read here for more details

You would end up with 2 migrations file like this:

class RemoveFieldsFromMt940Batches < ActiveRecord::Migration
def change
remove_column :mt940_batches, :account_number, :transaction_reference_number, :information_to_account_owner
end
end

class AddFieldsToMt940Batches < ActiveRecord::Migration
def change
add_column :mt940_batches, :created_by, :updated_by, :integer
end
end

Will removing a column with a Rails migration remove indexes associated with the column

No, unfortunately you have to remove the index manually from within your migration using the remove_index method.



Related Topics



Leave a reply



Submit