Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a column and change its type by migration same time

Tags:

In my general_exams table, I have a column named semester, type is string. Now I want to change its name to semester_id, type is integer. I have read about migration and it has available transformations:

  • rename_column(table_name, column_name, new_column_name): Renames a column but keeps the type and content.
  • change_column(table_name, column_name, type, options): Changes the column to a different type using the same parameters as add_column.

So, I create my migration file like this:

class RenameSemesterFromGeneralExams < ActiveRecord::Migration    def change     rename_column :general_exams, :semester, :semester_id     change_column :general_exams, :semester_id, :integer   end end 

But, when I run rake db:migrate, it has error:

==  RenameSemesterFromGeneralExams: migrating ================================= -- rename_column(:general_exams, :semester, :semester_id)    -> 0.0572s -- change_column(:general_exams, :semester_id, :integer) rake aborted! An error has occurred, this and all later migrations canceled:  PG::Error: ERROR:  column "semester_id" cannot be cast to type integer : ALTER TABLE "general_exams" ALTER COLUMN "semester_id" TYPE integer 

In my table GeneralExam, I destroyed all data. So, anyone can tell me how can I do that? Or I must create two migration files?

like image 712
Thanh Avatar asked Nov 20 '12 09:11

Thanh


People also ask

How do I rename a column in migration?

To rename a column, you may use the renameColumn method on the Schema builder. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer. json file: Schema::table('users', function (Blueprint $table) { $table->renameColumn('from', 'to'); });

Is it possible to change column name?

1. Renaming a column name using the ALTER keyword. Line 2: RENAME COLUMN OldColumnName TO NewColumnName; For Example: Write a query to rename the column name “SID” to “StudentsID”.

Can I rename migration file rails?

You can change the migration file name, but you have to perform a few steps: rake db:rollback to the point that queries table is rolled back. Now change the name of migration file, also the contents. Change the name of any Model that may use the table.


2 Answers

This works as of Rails 4

def change   rename_column :general_exams, :semester, :semester_id   change_column :general_exams, :semester_id, :integer end 
like image 118
wind Avatar answered Sep 25 '22 22:09

wind


Your problem is probably that the semester contains data that cannot be converted to integers. That's why you get a cast error.

I suspect you need to do more work to make this work as the only thing that comes to mind is removing the column and creating a new one with the correct values.

But you can simply remove_column and then add_column in one migration. That should work flawlessly.

I'd also suggest you only add_column first, then do the mapping process where you map the old semester value onto the new semester_id and then drop the column.

Keep in mind that you can do ActiveRecord manipulations inside your migration. So you can put that code in there.

like image 32
Tigraine Avatar answered Sep 26 '22 22:09

Tigraine