1
0

More than 1 year has passed since last update.

【Rails】PG::UndefinedTable: ERROR: relation "users" does not existの対処法【DB】

Last updated at Posted at 2022-11-09

症状

DockerでRailsの環境構築後にDB:migrateをしようとしたとき、下記のエラーが発生しました。
error
PG::DuplicateTable: ERROR:  relation "users" already exists

マイグレーションファイルは以下を使用していました。

マイグレーションファイル
20210817024851_photos
20220817024851_users
20210817024851_photos
class CreatePhotos < ActiveRecord::Migration[6.0]
  def change
    create_table :fhotos do |t|
      t.references :user, null: false, foreign_key: true
      t.string :name, null: false
      t.string :image

      t.boolean :deleted,default:false
      t.timestamps
    end
  end
end
20220817024851_users
class CreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :fhotos do |t|
      t.string :name, null: false

      t.boolean :deleted,default:false
      t.timestamps
    end
  end
end

解決策

userが先に読まれるように、マイグレーションファイルの名前をリネームして解決しました。

エラーが出ていた理由としては、上から順にマイグレーションファイルが処理されていたため、userに依存しているphotoが先に読まれてしまい、userがないとエラーが出ていたようです。

solution
20200817024851_users
20210817024851_photos

参考

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