Ruby 里表的增删改查
官网链接: Active Record Migrations — Ruby on Rails Guides
添加string字段
rails g migration AddFieldIdToTables field_id:string
添加boolean(tinyint)字段
rails g migration AddFieldFlagToTables field_flag:boolean
添加text字段
rails g migration AddFieldJsonToTables field_json:text
添加Integer字段
rails g migration AddFieldDaysToTables field_days:integer
删除string 字段
rails g migration RemoveEmailFromTables email:string
重命名Text字段为Integer(不建议修改数据类型,耗时较长,建议删除再新建)
rails g migration RenameFieldFromTables
class RenameFieldFromTables < ActiveRecord::Migration[5.2]
def change
rename_column :tables, :field, :field_id
change_column :tables, :field_id, :integer
end
end
好了之后不用忘记运行:
rake db:migrate