3
3

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 aborted! StandardError: An error has occurred, this and all later migrations canceled:

Posted at

初心者がrails tutorialでscaffoldでUserモデルを生成後、rails db:migrateを入力したときに遭遇したエラー処理について共有します。

mac-no-MacBookPro:toy_app mac$ ./qs rails db:migrate
Starting toy_app_db_1 ... done
== 20190413154520 CreateUsers: migrating ======================================
-- create_table(:users)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "users" already exists
: CREATE TABLE "users" ("id" bigserial primary key, "name" character varying, "email" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)

migrateした後にusers tableが既に存在すると表示されました。
そこで

$ rails db:migrate:reset
$ rails db:migrate

resetのコード入力後、再度migrateを試みるも状況は変わらず。

最終的には下記のようにエディタから直接いじり、解決できました。DBのmigrateファイルの中の今回DBに反映させたかった部分を一度コメントアウト。

class CreateMicroposts < ActiveRecord::Migration[5.2]
  def change
    #create_table :microposts do |t|
      #t.text :content
      #t.integer :user_id

      #t.timestamps
    #end
  end
end

その後

$ rails db:migrate

そして、この後はコメントアウトした「#」の文字を消します。

class CreateMicroposts < ActiveRecord::Migration[5.2]
  def change
    create_table :microposts do |t|
      t.text :content
      t.integer :user_id

      t.timestamps
    end
  end
end

そして最後に以下のコードを打ち込み、解決しました。

$ rails db:migrate:down VERSION= <該当のmigrateファイル名>
$ rails db:migrate
3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?