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

后端 未结 4 1432
迷失自我
迷失自我 2021-02-06 23:43

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

4条回答
  •  -上瘾入骨i
    2021-02-07 00:12

    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.

提交回复
热议问题