当我添加新列时,我可以按如下方式添加数据库注释。
add_column :foo_tables, :bar_column, :boolean, comment: 'comment'
然而,当我更改列名时,评论消失了,似乎无法再添加它... 我尝试了以下模式,但没有一个工作:(
# migration succeeds, but comment is gone
rename_column :foo_tables, :bar_column, :bar_new_column
# migration fails (rename_column does not take :comment as arg)
rename_column :foo_tables, :bar_column, :bar_new_column, comment: 'comment'
# migration fails (change_column does not take :comment as arg)
rename_column :foo_tables, :bar_column, :bar_new_column
change_column :foo_tables, comment: 'new_comment'
重命名列后有没有办法保留评论?
答案 0 :(得分:1)
您必须将代码的底部更改为:
rename_column :foo_tables, :bar_column, :bar_new_column
change_column :foo_tables, :bar_new_column, :string, comment: 'new_comment'
如果查看migration文档,可以看到rename_column
只需要3个参数:
rename_column(table_name,column_name,new_column_name)
在迁移中,您可以重命名该列,然后添加注释。