LoginSignup
0

posted at

updated at

【Rails】マイグレーションコマンド

コマンド

「bundle exec rails」は「bin/rails」でもOK

新規モデル作成

bundle exec rails g model モデル名 カラム名1:型 カラム名2:型

例えば「nameというstringのカラムだけを持つuserモデル」を作成する場合↓

bundle exec rails g model user name:string

これだけで下記ファイルを自動生成してくれる

      create    db/migrate/20220422143603_create_users.rb
      create    app/models/user.rb
      create      test/models/user_test.rb
      create      test/fixtures/users.yml

気になるマイグレファイルの作成結果

db/migrate/20220422143603_create_users.rb
class CreateUsers < ActiveRecord::Migration[7.0]
  def change
    create_table :users do |t|
      t.string :name

      t.timestamps
    end
  end
end

マイグレーションを適用する

bundle exec rails db:migrate

適用済みマイグレーションを確認

bundle exec rails db:migrate:status

マイグレーションを1つ戻す

bundle exec rails db:rollback

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
What you can do with signing up
0