Mysql2::Error: Table 'myapp_development.post_tags' doesn't exist のエラー解決
背景
postを削除しようとしたら、このエラーが起こった。
解決のプロセス
mysqlのテーブルを見てみる
mysql> use myapp_development Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-----------------------------+ | Tables_in_myapp_development | +-----------------------------+ | ar_internal_metadata | | comments | | impressions | | likes | | posts | | posts_tags | | relationships | | schema_migrations | | tags | | users | +-----------------------------+ 10 rows in set (0.01 sec) mysql>
schembaを見てみる
create_table "posts_tags", id: false, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "post_id", null: false t.bigint "tag_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["post_id"], name: "index_posts_tags_on_post_id" t.index ["tag_id"], name: "index_posts_tags_on_tag_id" end
post_tagsを削除しようとしているのはなぜ?
これが原因だった。
post.rb has_many :post_tags, dependent: :destroy
以前、タグを新しいテーブルを作成し、改良したときに、古いテーブルを消し忘れていた事が原因だった。