LoginSignup
1
0

More than 1 year has passed since last update.

【rails db:migrateに失敗】StandardError (An error has occurred, this and all later migrations canceled:PG::DuplicateTable: ERROR: relation "posts" already exists ):

Posted at

事象

マイグレーションの移行のため、rails db:migrateを実行すると、以下のエラーが発生。

StandardError (An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "posts" already exists
):

データベースにpostsが既に存在しているというもの。
前回一度rails db:migrateをしたが、一旦全てのアプリを削除し、今回は再作成している状況であったため、残っていたと想定。

解決方法

まずは、現状を以下で確認。

❯ rails db

前回作成したデータベースのpostsを削除するために以下を実行。
モードが切り替わり、post_bootstrap_app_development=#となるため、以下を\dを実行。
すると、以下のように、postsの存在を確認。

                  List of relations
 Schema |         Name         |   Type   |  Owner
--------+----------------------+----------+----------
 public | ar_internal_metadata | table    | ユーザ名
 public | posts                | table    | ユーザ名
 public | posts_id_seq         | sequence | ユーザ名
 public | schema_migrations    | table    | ユーザ名
(4 rows)

このpostsを削除するため、以下を実行

drop table posts;

そして、\dで確認すると、

                List of relations
 Schema |         Name         | Type  |  Owner
--------+----------------------+-------+----------
 public | ar_internal_metadata | table | ユーザ名
 public | schema_migrations    | table | ユーザ名
(2 rows)

ちゃんと、postが消えていることを確認。
そして、再度マイグレーションを実行。

❯ rails db:migrate

rails sでサーバ起動し、正しい動作を確認できた。

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