💻
Model(マイグレーション済み)の削除方法
モデル名を間違えて作成してしまいました。
誤りに気付かずマイグレーションを行ってしまったので、
ケアのための操作も行いました。
マイグレーションをup→downに切り替える
Statusをdownに切り替えます。
upのままだと、モデルを削除してもマイグレーションに残ってしまうので、
のちにrails db:migrateを行なったときにエラーが起きてしまいます。
コマンド
$ bundle exec rake db:migrate:status
downにしたいMigration IDを確認します。
ターミナル
database: db/development.sqlite3
Status Migration ID Migration Name
--------------------------------------------------
up 20210727092511 Devise create users
up 20210727093149 Create books
up 20211021071114 Create active storage tablesactive storage
up 20230922015059 Create favorites
up 20230923045902 Create relationships
up 20231004071228 Create post comments
今回は一番下のpost comments
のStatusをdownさせます。
コマンド
$ bundle exec rake db:migrate:down VERSION=20231004071228_post_comments.rb
VERSION=
以降の数字は先ほど確認したStatus画面のMigration IDを参照に入力しています。
これでpost comments
Sutatusはdownに切り替わります。
ターミナル
database: db/development.sqlite3
Status Migration ID Migration Name
--------------------------------------------------
up 20210727092511 Devise create users
up 20210727093149 Create books
up 20211021071114 Create active storage tablesactive storage
up 20230922015059 Create favorites
up 20230923045902 Create relationships
down 20231004071228 Create post comments
モデルの削除
コマンド
$ rails destroy model PostComment
モデルを削除するコマンドを入力します。
ターミナル
Running via Spring preloader in process 21941
invoke active_record
remove db/migrate/20231004093402_create_post_comments.rb
remove app/models/post_comment.rb
invoke test_unit
remove test/models/post_comment_test.rb
remove test/fixtures/post_comments.yml
以上のように表示されていれば完了です。
Discussion