1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails / Active Record / Migration よく使うものまとめ

Posted at

概要

環境

まとめ一覧

テーブルにNOT NULL制約とデフォルト値制約をつけたカラムを追加

(例) Sampleモデルに「デフォルト値 0」「NOT NULL」 制約をつけた Integer型の sample_columnカラムを追加する場合

YYYYMMDDhhmmss_samples.rb
class Samples < ActiveRecord::Migration[5.1]
  def change
    add_column :samples, :sample_column, :integer
    change_column_default :samples, :sample_column, from: nil, to: 0
    change_column_null  :samples, :sample_column, false, 0
  end
end
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?