0
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 3 years have passed since last update.

Rails 既存カラムにdefault オプションを追加

Posted at

##1.使用環境
mac.os Catalina バージョン10.15.7

Ruby 3.0.1
Rails 6.0.4.1
psql (PostgreSQL) 13.4
node 14.18.10
npm 7.24.0

##2.既存のカラムにdefaultオプションついか


1.追加用のテーブルを新たに作成します
usersテーブルのadminカラムに追加する例です。

rails g migration change_admin_to_users

2. 先ほど作成したdbファイルに #####change_column :テーブル名,  :カラム名,  :データ型, null: false, default: false
class ChengeAdminToUsers < ActiveRecord::Migration[6.0]
  def change
    change_column :users, :admin, :boolean, null: false, default: false
  end          
end

ターミナルで下記を実行

rails db:migrate

カラム追加・変更・削除は [こちら](https://310nae.com/rails-migration/) を参考にしてみてください!
0
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
0
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?