プログラミング学習記録

自動車メーカー勤務しながらプログラミング学習中。学習記録を残していきます。

ActiveRecord::PendingMigrationErrorの解決について

 Versions

$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin19]
$ rails -v
Rails 5.2.6

 エラー内容

rubyrailsインストール後、scaffold_app作成して bin/rails sをしたが以下のエラーが出る

ActiveRecord::PendingMigrationError


Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development


 # Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
      def check_pending!(connection = Base.connection)
        raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration?
      end

      def load_schema_if_pending!

やったこと

 ①エラー内容の通りに実行

$ bin/rails db:migrate RAILS_ENV=development

 ②既存のテーブルを全削除し、再度マイグレーションの状態をupにする

https://qiita.com/KONTA2019/items/0444ae3b8c8936a56ee0

$ bin/rails db:migrate:reset

 ③最新マイグレーションの実行を取り消す=(down)にするrails db:rollback、マイグレーションファイル更新rails db:migrate

$ rails db:rollback
$ rails db:migrate

 ④rails のmigrationの状態を確認

$ bin/rails db:migrate
$ bin/rails db:migrate:status


Status   Migration ID    Migration Name
--------------------------------------------------
  down    20210609073651  Create users

db:migrateしたのにstatusを調べるとdownになっている…

 ⑤migrateファイル確認

class CreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      t.string, :name
      t.string, :address
      t.integer :age

      t.timestamps
    end
  end
end

t.string, :nameと t.string, :addressに「,」が入っているのが原因かもしれない。 「,」を消してdb:migrate後にサーバー立ち上げてみる。

class CreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      t.string :name
      t.string :address
      t.integer :age

      t.timestamps
    end
  end
end
$ bin/rails db:migrate
$ bin/rails s

うあーーーサーバー立ち上がったー