質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2257閲覧

An error has occurred, all later migrations canceled: Mysql2::Error: Table 'likes' already existsの対処

Shoukenn

総合スコア6

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/09/29 12:55

編集2020/09/29 15:37

Likeというテーブルをを作成時には、かつて作成したことがあったので、以下のように先に

rails d model like

と削除し、また

rails g model like article:references user:references

と打って新しいテーブルを作成したと思いきや、

rails db:migrate

をするときに、このようなエラーがでてきました。

C:\Users\76361\Desktop\shikakuichannel2>rails db:migrate == 20200929115331 CreateLikes: migrating ====================================== -- create_table(:likes) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Table 'likes' already exists C:/Users/76361/Desktop/shikakuichannel2/db/migrate/20200929115331_create_likes.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Table 'likes' already exists C:/Users/76361/Desktop/shikakuichannel2/db/migrate/20200929115331_create_likes.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Caused by: Mysql2::Error: Table 'likes' already exists C:/Users/76361/Desktop/shikakuichannel2/db/migrate/20200929115331_create_likes.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)

正直に言うと前回も類似な問題でやり直しに狭められたが、
今回はロールバックなど、慎重のためやってないですが、
何にが対処方法がありますか。
イメージ説明

rails db:migrate RAILS_ENV=development

も打ったが

== 20200929115331 CreateLikes: migrating ====================================== -- create_table(:likes) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Table 'likes' already exists C:/Users/76361/Desktop/shikakuichannel2/db/migrate/20200929115331_create_likes.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Table 'likes' already exists C:/Users/76361/Desktop/shikakuichannel2/db/migrate/20200929115331_create_likes.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Caused by: Mysql2::Error: Table 'likes' already exists C:/Users/76361/Desktop/shikakuichannel2/db/migrate/20200929115331_create_likes.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)

の示されるように変化がないようです。

db/migrate/20200929115331_create_likes.rb

class CreateLikes < ActiveRecord::Migration[6.0] def change create_table :likes do |t| t.references :article, null: false, foreign_key: true t.references :user, null: false, foreign_key: true t.timestamps end end end

db/schema.rb

# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `rails # db:schema:load`. When creating a new database, `rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2020_09_23_020250) do create_table "articles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "title" t.text "content" t.string "category" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" end create_table "comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "title" t.text "content" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" t.integer "article_id" end create_table "likes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" t.integer "article_id" end create_table "user2s", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "name" t.text "profile" t.string "email" t.string "password" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hatsu

2020/09/29 14:37

今回はテーブルがすでに作られているので作り直したい?またはロールバックをしなくていいようにカラムを追加したい?どの様になるのが理想でしょうか。状況にもよりますが、bundle exzec rails db:migrate:status などでLikesテーブルが作られたタイミングを確認して戻れそうなら戻るのがいいと思っています。
Shoukenn

2020/09/29 14:55

質問文が簡略化過ぎるのですみません。もともとrails g model likeで作ったんだけど、上記のrails g model like article:references user:referencesに従っていないためいったんrails d model likeで削除したが、その後rails g model like article:references user:referencesで作り直した。けれども、rails db:migrate実行する時にエラーが出て、どこで間違えているかが分からなくて、対処方法について聞かせたいと思います。
Shoukenn

2020/09/29 14:58

さっきbundle exec rails db:migrate:status実行したところ、以下のようなステータスが示されている。 Status Migration ID Migration Name -------------------------------------------------- up 20200923004754 Devise create users up 20200923005503 Create user2s up 20200923010617 Create articles up 20200923010731 Create comments up 20200923010804 ********** NO FILE ********** up 20200923015815 Add user id to articles up 20200923015949 Add user id to comments up 20200923020116 Add article id to comments up 20200923020202 Add user id to likes up 20200923020250 Add article id to likes down 20200929115331 Create likes
hatsu

2020/09/29 15:16

down 20200929115331 Create likes これ見ると、作ったlikesテーブルをrollbackで戻しているようですね。コノタイミングでもTable 'likes' already exists と出ますでしょうか?出る場合はMigate以外で作ってしまってたりですかね。。念のため、db/migrate/20200929115331_create_likes.rb のファイルの中身とdb/schema.rbをみたいです。
Shoukenn

2020/09/29 15:27

はい、確かにこのタイミングで"Table 'likes' already exists"のエラーが出てしまいました。 db/migrate/20200929115331_create_likes.rb: class CreateLikes < ActiveRecord::Migration[6.0] def change create_table :likes do |t| t.references :article, null: false, foreign_key: true t.references :user, null: false, foreign_key: true t.timestamps end end end db/schema.rb: # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `rails # db:schema:load`. When creating a new database, `rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2020_09_23_020250) do create_table "articles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "title" t.text "content" t.string "category" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" end create_table "comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "title" t.text "content" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" t.integer "article_id" end create_table "likes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" t.integer "article_id" end create_table "user2s", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "name" t.text "profile" t.string "email" t.string "password" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end
hatsu

2020/09/29 15:29

あ、コードは質問文に追加いただけると見やすくて助かりますmm
Shoukenn

2020/09/29 15:31

ちなみに、今回は"rails db:rollback"やっていません。 前回はまさにロールバックにより、最終的に解決不能となってしまって、 元のプロジェクトを手放さざるを得なかったです。(;´・ω・)
Shoukenn

2020/09/29 15:37

質問文に新たに追加しました、恐れ入ります。(;´・ω・)
guest

回答1

0

ベストアンサー

schema.rbにlikesの記述があり、commentsとuser2sの間であることを考慮すると、
db:migrate:statusの結果の

up 20200923010804 ********** NO FILE **********

このタイミングでlikesテーブルを作り、ファイルだけ消したと思われます。
まずはその解消を、例えば手順でできますでしょうか。
https://qiita.com/sakatan_1/items/9bf321f81d3b84042694

ファイルを作って以下のコマンドが実行できたら解消できるかもです。

rails db:migrate:down VERSION=20200923010804

投稿2020/09/29 15:50

hatsu

総合スコア1809

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Shoukenn

2020/09/29 16:17

さっき試したところ、残念ながら相変わらず、"rails db:migrate"を実行しても同じエラーが出てしまいました。( TДT) ちなみに、実行したステータスはこちら: database: shikakuichannel2_development Status Migration ID Migration Name -------------------------------------------------- up 20200923004754 Devise create users up 20200923005503 Create user2s up 20200923010617 Create articles up 20200923010731 Create comments down 20200923010804 Hoge up 20200923015815 Add user id to articles up 20200923015949 Add user id to comments up 20200923020116 Add article id to comments up 20200923020202 Add user id to likes up 20200923020250 Add article id to likes down 20200929115331 Create likes
Shoukenn

2020/09/29 16:23

そもそも20200923010804_hoge.rbを削除する必要がありますか?
hatsu

2020/09/29 16:26

あー作っていただいたHogeってファイルでStatusをDownにできたのですが肝心のlikesテーブルの削除ができていないためですね。 1. rails db:migrate:up VERSION=20200923010804 にします 2. hogeのファイルに以下を書きます ``` class CreateLikes < ActiveRecord::Migration[6.0] def up create_table :likes do |t| t.references :article, null: false, foreign_key: true t.references :user, null: false, foreign_key: true t.timestamps end def down drop_table :likes end end ``` 3. rails db:migrate:down VERSION=20200923010804 これでdownにしたときにlikesテーブルの削除が実行されるかと思います。 4. その後またrails db:migrate:up VERSION=20200923010804 でcreateテーブルが走りますでしょうか。それかhogeのファイルを削除して現在実行しようとしているファイルでcreate tableでも大丈夫かと思います。
Shoukenn

2020/09/29 16:36

何度も本当にすみません。 "rails db:migrate:down VERSION=20200923010804"実行後、 以下のようなエラーが出てしまいました。 rails aborted! SyntaxError: C:/Users/76361/Desktop/shikakuichannel2/db/migrate/20200923010804_hoge.rb:13: syntax error, unexpected end-of-input, expecting end bin/rails:4:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate:down (See full trace by running task with --trace)
hatsu

2020/09/29 16:38

t.timestamps end この下にendが足りてないとかだと思います。
Shoukenn

2020/09/29 16:39

ちなみに、今現在HogeはUPのままです。
Shoukenn

2020/09/29 16:43

endを足しましたが、今回はNameErrorです。 rails aborted! NameError: uninitialized constant Hoge bin/rails:4:in `require' bin/rails:4:in `<main>' Caused by: NameError: uninitialized constant Hoge bin/rails:4:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate:down (See full trace by running task with --trace)
Shoukenn

2020/09/29 16:53

夜遅くまで申し訳ございません。そもそも限界にいたったので、寝なくてはいけないが、明日も引き続き解決するつもりですので、気をなさらずにどうぞお休みください。
hatsu

2020/09/30 01:40

uninitialized constant Hogeて言われているので、 class Hoge < ActiveRecord::Migration[6.0] にするといいと思います
Shoukenn

2020/09/30 07:42

長らく返事しなくて申し訳ございません。実はその後何回も"rails db:rollback"して、一番下の”Create Likes”のファイルを削除して解決しました。長らくお付き合いいただきありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問