How to remove a column from my Rails model? - ruby-on-rails

I need to remove a few columns from my rails model which i already created and have some row entries in that model.
How to do it? Any links which has details for modifying the schema in rails ?
I'm using rails version 3.

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.

If you know the columns you want to remove you can use the convention: Remove..From.. when naming your migrations. Additionally you can include the column names when running the migration command.
The form of the command:
rails g migration Remove..From.. col1:type col2:type col3:type
For example:
rails g migration RemoveProjectIDFromProjects project_id:string
generates the following migration file:
class RemoveProjectIdFromProjects < ActiveRecord::Migration
def self.up
remove_column :projects, :project_id
end
def self.down
add_column :projects, :project_id, :string
end
end

Via command alternative as Add, only change Add to Remove:
Single Column:
rails g migration RemoveColumnFromTable column:type
Multiple Columns:
rails g migration RemoveColumn1AndColumn2FromTable column1:type colummn2:type

Related

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

What is the command for removing an existing column from a table using migration?
The column I want to remove is: country:string
From the table: sample_apps
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
Create migration file:
$ rails generate migration RemoveCountryFromSampleApps country:string
In generated migration file:
class RemoveCountryFromSampleApps < ActiveRecord::Migration
def change
remove_column :sample_apps, :country, :string
end
end
Then run:
rake db:migrate
To remove a column(country here) from table(sample_case)
rails generate migration RemoveCountryfromSampleCase country:string
Above command should generate a file YYYYMMDDHHMMSS_remove_countryfrom_sample_case.rb. under db/migrate folder
class RemoveCountryFromSampleCase < ActiveRecord::Migration[5.0]
def change
remove_column :sample_case, :country, :string
end
end
In my case (I was doing it for more than two columns) only this appears
class RemoveCountryFromSampleCase < ActiveRecord::Migration[5.0]
def change
end
end
remove_column line was not there so I added it manually and then fired the command
rails db:migrate
and it worked for me.
References https://stackoverflow.com/a/2963582
&
Ruby guide on Active Record migration
https://edgeguides.rubyonrails.org/active_record_migrations.html
If want to remove the index too, you do with migration too:
rails g migration remove_post_id_from_comments post_id:integer:index
migration file:
class RemovePostIdFromComments < ActiveRecord::Migration
def change
remove_index :comments, :post_id
remove_column :comments, :post_id, :integer
end
end
then run: rake db:migrate
Do you need to specify the type?
Why not just remove_column :sample_apps, :country or remove_column :comments, :post_id for the samples here? Seems to work and removes a chance for error (text or string).

Adding column to database table in rails 1.2.6(legacy version)

I am using ruby 1.8.7 and rails 1.2.6. (Its old I know. But I have to use it.) I need to add column to users table. I cant use rails generate migration with rails 1.2.6. I need to add a versioned db migrate file. How can I do that?
I want to add product column to users table. I created a file in the db/migrate folder with following contents.
class AddProductToUser < ActiveRecord::Migration
def self.up
add_column :users, :product, :string
end
def self.down
remove_column :users, :product
end
end
I used script/generate migration AddProductToUser. It gives an error as
undefined method 'cache' for Gem:Module.
Any pointers on how to run migration in rails 1.2.6(<2.x) will also be useful.
Your migrate file looks (almost) fine, does the filename match the class name, and does it have a sequence number at the beginning that follows on from the previous migrate?
Slight change:
# db/migrate/123_add_product_to_user.rb
class AddProductToUser < ActiveRecord::Migration
def self.up
add_column :users, :product, :string
end
def self.down
remove_column :users, :product # note sure where radius came from?
end
end
Then should just be run with rake migrate
More info at apidock

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

HI I created a Ruby on rails migration file as follows and in the first stage I created tables
then I want to add columns and remove some columns and I modified it as follows
class CreateMt940Batches < ActiveRecord::Migration
def change
create_table :mt940_batches do |t|
t.string :account_number
t.string :transaction_reference_number
t.string :information_to_account_owner
t.string :file_name
t.binary :raw_data_transaction
t.string :sha1_checksum
t.timestamps
end
def self.down
remove_column :account_number, :transaction_reference_number, :information_to_account_owner
end
def self.up
add_column :mt940_batches, :created_by, :updated_by, :integer
end
end
end
but when I ran rake db:migrate nothing has happens. How to accomplish this task . I want to change the model already created as well from this migration file. Um looking a way to do this. Thank you in advance
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
Do not edit it if this migration already executed in production env create new one instead
if not you can use rake db:rollback, rollback migrations
Because this migration is already executed. you have to generate a new migration for adding and removing column in your table, i.e. you want to remove file_name from your table :
run this:
rails g migration RemoveFileNameFromCreateMt940Batches file_name:string
re-generate that column:
rails g migration AddFileNameToCreateMt940Batches file_name:string
Than run rake db:migrate it will remove column and add column again to your table.
Hope it will help. Thanks.
Create another migration file with removed column list
def change
remove_column :account_number, :transaction_reference_number, :information_to_account_owner
end
Create one migration file with added column list
def change
add_column :mt940_batches, :created_by, :updated_by, :integer
end
Do not alter the create table migration file. Other wise data saved in the file will be lost.
If data lost is not important for you, then just remove the table using rake db:migrate:down version=<your migration file version>
And change the migration file
then run
db:migrate:up version=<your migration file version>

Rails: Adding an index after adding column

Suppose I created a table table in a Rails app. Some time later, I add a column running:
rails generate migration AddUser_idColumnToTable user_id:string.
Then I realize I need to add user_id as an index. I know about the add_index method, but where should this method be called? Am I supposed to run a migration (if yes, which one ?), then adding by hand this method?
You can run another migration, just for the index:
class AddIndexToTable < ActiveRecord::Migration
def change
add_index :table, :user_id
end
end
If you need to create a user_id then it would be a reasonable assumption that you are referencing a user table. In which case the migration shall be:
rails generate migration AddUserRefToProducts user:references
This command will generate the following migration:
class AddUserRefToProducts < ActiveRecord::Migration
def change
add_reference :user, :product, index: true
end
end
After running rake db:migrate both a user_id column and an index will be added to the products table.
In case you just need to add an index to an existing column, e.g. name of a user table, the following technique may be helpful:
rails generate migration AddIndexToUsers name:string:index will generate the following migration:
class AddIndexToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string
add_index :users, :name
end
end
Delete add_column line and run the migration.
In the case described you could have issued rails generate migration AddIndexIdToTable index_id:integer:index command and then delete add_column line from the generated migration. But I'd rather recommended to undo the initial migration and add reference instead:
rails generate migration RemoveUserIdFromProducts user_id:integer
rails generate migration AddUserRefToProducts user:references
Add in the generated migration after creating the column the following (example)
add_index :photographers, :email, :unique => true
For references you can call
rails generate migration AddUserIdColumnToTable user:references
If in the future you need to add a general index you can launch this
rails g migration AddOrdinationNumberToTable ordination_number:integer:index
Generated code:
class AddOrdinationNumberToTable < ActiveRecord::Migration
def change
add_column :tables, :ordination_number, :integer
add_index :tables, :ordination_number, unique: true
end
end
You can use this, just think Job is the name of the model to which you are adding index cader_id:
class AddCaderIdToJob < ActiveRecord::Migration[5.2]
def change
change_table :jobs do |t|
t.integer :cader_id
t.index :cader_id
end
end
end
For those who are using postgresql db and facing error
StandardError: An error has occurred, this and all later migrations canceled:
=== Dangerous operation detected #strong_migrations ===
Adding an index non-concurrently blocks writes
please refer this article
example:
class AddAncestryToWasteCodes < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def change
add_column :waste_codes, :ancestry, :string
add_index :waste_codes, :ancestry, algorithm: :concurrently
end
end

How can I rename a database column in a Ruby on Rails migration?

I wrongly named a column hased_password instead of hashed_password.
How do I update the database schema, using migration to rename this column?
rename_column :table, :old_column, :new_column
You'll probably want to create a separate migration to do this. (Rename FixColumnName as you will.):
bin/rails generate migration FixColumnName
# creates db/migrate/xxxxxxxxxx_fix_column_name.rb
Then edit the migration to do your will:
# db/migrate/xxxxxxxxxx_fix_column_name.rb
class FixColumnName < ActiveRecord::Migration
def self.up
rename_column :table_name, :old_column, :new_column
end
def self.down
# rename back if you need or do something else or do nothing
end
end
For Rails 3.1 use:
While, the up and down methods still apply, Rails 3.1 receives a change method that "knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method".
See "Active Record Migrations" for more information.
rails g migration FixColumnName
class FixColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again:
rename_column :table_name, :old_column1, :new_column1
rename_column :table_name, :old_column2, :new_column2
...
You could use change_table to keep things a little neater:
class FixColumnNames < ActiveRecord::Migration
def change
change_table :table_name do |t|
t.rename :old_column1, :new_column1
t.rename :old_column2, :new_column2
...
end
end
end
Then just db:migrate as usual or however you go about your business.
For Rails 4:
While creating a Migration for renaming a column, Rails 4 generates a change method instead of up and down as mentioned in the above section. The generated change method is:
$ > rails g migration ChangeColumnName
which will create a migration file similar to:
class ChangeColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
In my opinion, in this case, it's better to use rake db:rollback, then edit your migration and again run rake db:migrate.
However, if you have data in the column you don't want to lose, then use rename_column.
If the column is already populated with data and live in production, I'd recommend a step by step approach, so as to avoid downtime in production while waiting for the migrations.
First I'd create a db migration to add columns with the new name(s) and populate them with the values from the old column name.
class AddCorrectColumnNames < ActiveRecord::Migration
def up
add_column :table, :correct_name_column_one, :string
add_column :table, :correct_name_column_two, :string
puts 'Updating correctly named columns'
execute "UPDATE table_name SET correct_name_column_one = old_name_column_one, correct_name_column_two = old_name_column_two"
end
end
def down
remove_column :table, :correct_name_column_one
remove_column :table, :correct_name_column_two
end
end
Then I'd commit just that change, and push the change into production.
git commit -m 'adding columns with correct name'
Then once the commit has been pushed into production, I'd run.
Production $ bundle exec rake db:migrate
Then I'd update all of the views/controllers that referenced the old column name to the new column name. Run through my test suite, and commit just those changes. (After making sure it was working locally and passing all tests first!)
git commit -m 'using correct column name instead of old stinky bad column name'
Then I'd push that commit to production.
At this point you can remove the original column without worrying about any sort of downtime associated with the migration itself.
class RemoveBadColumnNames < ActiveRecord::Migration
def up
remove_column :table, :old_name_column_one
remove_column :table, :old_name_column_two
end
def down
add_column :table, :old_name_column_one, :string
add_column :table, :old_name_column_two, :string
end
end
Then push this latest migration to production and run bundle exec rake db:migrate in the background.
I realize this is a bit more involved of a process, but I'd rather do this than have issues with my production migration.
See the "Available Transformations" section in the "Active Record Migrations" documentation.
rename_column(table_name, column_name, new_column_name):
Renames a column but keeps the type and content.
Run this command to create a migration file:
rails g migration ChangeHasedPasswordToHashedPassword
Then in the file generated in the db/migrate folder, write rename_column as below:
class ChangeOldColumnToNewColumn < ActiveRecord::Migration
def change
rename_column :table_name, :hased_password, :hashed_password
end
end
From the API:
rename_column(table_name, column_name, new_column_name)
This renames a column but keeps the type and content remains the same.
I had this challenge when working on a Rails 6 application with a PostgreSQL database.
Here's how I fixed it:
In my case the table_name was "Products", the old_column was "SKU" and the new_column was "ProductNumber".
Create a migration file that will contain the command for renaming the column:
rails generate migration RenameSKUToProductNumberInProducts
Open the migration file in the db/migrate directory:
db/migrate/20201028082344_rename_sku_to_product_number_in_products.rb
Add the command for renaming the column:
class RenameSkuToProductNumberInProducts < ActiveRecord::Migration[6.0]
def change
# rename_column :table_name, :old_column, :new_column
rename_column :products, :sku, :product_number
end
end
Save, and then run the migration command:
rails db:migrate
You can now confirm the renaming of the column by taking a look at the schema file:
db/schema.rb
If you are not satisfied with the renaming of the column, you can always rollback:
rails db:rollback
Note: Endeavour to modify the column name to the new name in all the places where it is called.
If your code is not shared with other one, then best option is to do just rake db:rollback
then edit your column name in migration and rake db:migrate. Thats it
And you can write another migration to rename the column
def change
rename_column :table_name, :old_name, :new_name
end
Thats it.
Some versions of Ruby on Rails support the up/down methods for migration and if you have an up/down method in your migration, then:
def up
rename_column :table_name, :column_old_name, :column_new_name
end
def down
rename_column :table_name, :column_new_name, :column_old_name
end
If you have the change method in your migration, then:
def change
rename_column :table_name, :column_old_name, :column_new_name
end
For more information see: Ruby on Rails - Migrations or Active Record Migrations.
As an alternative option, if you are not married to the idea of migrations, there is a compelling gem for ActiveRecord which will handle the name changes automatically for you, Datamapper style. All you do is change the column name in your model, and make sure you put Model.auto_upgrade! at the bottom of your model.rb, and viola! The database is updated on the fly.
See https://github.com/DAddYE/mini_record
Note: You will need to nuke db/schema.rb to prevent conflicts.
It is still in the beta phase and obviously not for everyone, but it is still a compelling choice. I am currently using it in two non-trivial production apps with no issues.
For Ruby on Rails 4:
def change
rename_column :table_name, :column_name_old, :column_name_new
end
If you need to switch column names you will need to create a placeholder to avoid a "duplicate column name" error. Here's an example:
class SwitchColumns < ActiveRecord::Migration
def change
rename_column :column_name, :x, :holder
rename_column :column_name, :y, :x
rename_column :column_name, :holder, :y
end
end
If the present data is not important for you, you can just take down your original migration using:
rake db:migrate:down VERSION='YOUR MIGRATION FILE VERSION HERE'
Without the quotes, then make changes in the original migration and run the up migration again by:
rake db:migrate
Simply create a new migration, and in a block, use rename_column as below.
rename_column :your_table_name, :hased_password, :hashed_password
Generate the migration file:
rails g migration FixName
which creates db/migrate/xxxxxxxxxx.rb.
Edit the migration to do your will:
class FixName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
Let's KISS. All it takes is three simple steps. The following works for Rails 5.2.
1 . Create a Migration
rails g migration RenameNameToFullNameInStudents
rails g RenameOldFieldToNewFieldInTableName - that way it is perfectly clear to maintainers of the code base later on. (use a plural for the table name).
2. Edit the migration
# I prefer to explicitly write theupanddownmethods.
# ./db/migrate/20190114045137_rename_name_to_full_name_in_students.rb
class RenameNameToFullNameInStudents < ActiveRecord::Migration[5.2]
def up
# rename_column :table_name, :old_column, :new_column
rename_column :students, :name, :full_name
end
def down
# Note that the columns are reversed
rename_column :students, :full_name, :name
end
end
3. Run your migrations
rake db:migrate
And you are off to the races!
Manually we can use the below method:
We can edit the migration manually like:
Open app/db/migrate/xxxxxxxxx_migration_file.rb
Update hased_password to hashed_password
Run the below command
$> rake db:migrate:down VERSION=xxxxxxxxx
Then it will remove your migration:
$> rake db:migrate:up VERSION=xxxxxxxxx
It will add your migration with the updated change.
Run rails g migration ChangesNameInUsers (or whatever you would like to name it)
Open the migration file that has just been generated, and add this line in the method (in between def change and end):
rename_column :table_name, :the_name_you_want_to_change, :the_new_name
Save the file, and run rake db:migrate in the console
Check out your schema.db in order to see if the name has actually changed in the database!
Hope this helps :)
def change
rename_column :table_name, :old_column_name, :new_column_name
end
Generate a Ruby on Rails migration:
$:> rails g migration Fixcolumnname
Insert code in the migration file (XXXXXfixcolumnname.rb):
class Fixcolumnname < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
$: rails g migration RenameHashedPasswordColumn
invoke active_record
create db/migrate/20160323054656_rename_hashed_password_column.rb
Open that migration file and modify that file as below(Do enter your original table_name)
class RenameHashedPasswordColumn < ActiveRecord::Migration
def change
rename_column :table_name, :hased_password, :hashed_password
end
end
In the console:
rails generate migration newMigration
In the newMigration file:
class FixColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
Open your Ruby on Rails console and enter:
ActiveRecord::Migration.rename_column :tablename, :old_column, :new_column
You have two ways to do this:
In this type it automatically runs the reverse code of it, when rollback.
def change
rename_column :table_name, :old_column_name, :new_column_name
end
To this type, it runs the up method when rake db:migrate and runs the down method when rake db:rollback:
def self.up
rename_column :table_name, :old_column_name, :new_column_name
end
def self.down
rename_column :table_name,:new_column_name,:old_column_name
end
I'm on rails 5.2, and trying to rename a column on a devise User.
the rename_column bit worked for me, but the singular :table_name threw a "User table not found" error. Plural worked for me.
rails g RenameAgentinUser
Then change migration file to this:
rename_column :users, :agent?, :agent
Where :agent? is the old column name.
You can write a migration run the below command to update the column name:
rename_column :your_table_name, :hased_password, :hashed_password
Also, make sure that you update any usage of the old column name in your code with the new one.
A close cousin of create_table is change_table, used for changing existing tables. It is used in a similar fashion to create_table but the object yielded to the block knows more tricks. For example:
class ChangeBadColumnNames < ActiveRecord::Migration
def change
change_table :your_table_name do |t|
t.rename :old_column_name, :new_column_name
end
end
end
This way is more efficient if we use it with other alter methods such as: remove/add index/remove index/add column. We can do things like:
Rename
t.rename :old_column_name, :new_column_name
Add column
t.string :new_column
Remove column
t.remove :removing_column
Index column
t.index :indexing_column
rails g migration migrationName
So you go to your generated migration and add:
rename_column :table, :old_column, :new_column
to the method
First you need to run
rails g migration create_new_column_in_tablename new_column:datatype
rails g migration remove_column_in_tablename old_column:datatype
and then you need to check db/migration
you can check the details in the nem migration, if all the details is correct you need to run:
rails db:migrate
Just generate the migration using:
rails g migration rename_hased_password
After that edit the migration and add the following line in the change method:
rename_column :table, :hased_password, :hashed_password
This should do the trick.

Resources