LoginSignup
2
1

More than 5 years have passed since last update.

Migration の書き方まとめ。(MySQL)

Posted at

新規テーブル作成

create_table :articles do |t|
  t.integer :user_id
  t.string  :title
  t.text    :article
  t.timestamps
end

integer はデフォルトでカラム長は int(11)
string は varchar(255)
text は text 型

テーブル削除

drop_table :users

カラム追加

add_column :users, :delete_flg, :integer, defalut: 0

カラム削除

remove_column :users, :delete_flg

カラム名変更

rename_column :users, :delete_flg, :del_flg

カラム変更

change_column :users, :admin, :boolean, :default => false

カラム変更時は遡及も同時に行う

User.all.update_all(admin: false)

インデックス追加

add_index :users, [:name, :email], :unique => true, :name => 'users_ix1'

インデックス削除

remove_index :users, :users_ix1
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1