rails g modelでモデルを作った後、rails db:migrateしたらエラーが出たのでその時の対処法をメモしておきます。
エラー内容
--------------------------------------------------------------------------
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'notifications' already exists: CREATE TABLE `notifications` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `visiter_id` int NOT NULL, `visited_id` int NOT NULL, `post_id` int, `comment_id` int, `action` varchar(255) DEFAULT '' NOT NULL, `checked` tinyint(1) DEFAULT FALSE NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL)
--------------------------------------------------------------------------
もうすでにこのファイルは存在していると言われているみたいです。
1回モデルを消したときにテーブルが残っていたのかも?
対処方法
最初にモデルを削除します
$ rails destroy model Notification
次にテーブル削除のために削除用のmigrationファイルを作成します(ファイル名はなんでも良いです)
$ rails generate migration drop_table_notifications
作成したmigrationファイルにテーブル削除を記述する
class DropTableNotifications < ActiveRecord::Migration[5.2]
def change
drop_table :notifications
end
end
最後にマイグレーションを実行します
$ rails db:migrate
これでちゃんとテーブルも削除できて、無事モデル作成後、rails db:migrateも通りました!
参考にした記事
https://note.com/oreno/n/n45f8208ade29
とても分かりやすかったです!ありがとうございました!