0
0

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 3 years have passed since last update.

rails db:migrateでのエラー

Last updated at Posted at 2020-07-27

#プログラミングの勉強日記
2020年7月28日 Progate Lv.226
Railsでアプリケーションを作成中

#rails db:migrateを実行したときのエラー
 ターミナルでrails db:migarateを実行したときに下記のようなエラーが出てきた。

user@itpro2_ubuntu:~/travel$ rails db:migrate
== 20200727142030 AddUserRefToPost: migrating =================================
-- add_reference(:posts, :user, {:foreign_key=>true})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: user_id: ALTER TABLE "posts" ADD "user_id" integer
/home/user/travel/db/migrate/20200727142030_add_user_ref_to_post.rb:3:in `change'
/home/user/travel/bin/rails:9:in `require'
/home/user/travel/bin/rails:9:in `<top (required)>'
/home/user/travel/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

以下省略

#原因と解決方法
 この前のコマンドではマイグレーションで何らかのエラーが起きてしまっている。

データベースのリセットを行う
$ rails db:migrate:reset

$ rails db:migrate

rails db:migrate:resetでデータベースのリセットを行うと、データベースの中身が消えてしまうので注意する

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?