LoginSignup
0

posted at

updated at

【Rails】To avoid mistakes, drop_table is only reversible if given options or a block (can be empty).エラーの対処法

やりたいこと

# rails db:migrate:down VERSION=20220404233925
で指定したマイグレーションファイルをdownさせたい
→最終的にはマイグレーションファイルを削除したい

エラー内容

# rails db:migrate:down VERSION=20220404233925
== 20220404233925 Masters: reverting ==========================================
rails aborted!
StandardError: An error has occurred, all later migrations canceled:



To avoid mistakes, drop_table is only reversible if given options or a block (can be empty).

(省略)

調査

https://prograshi.com/framework/rails/rollback_drop-table_error/#toc1
↑の記事がめちゃくちゃ役に立ちました

原因

drop_tableでテーブルを削除するにあたって、ブロック形式を使わないとエラーになるそうです。
ブロック形式を使うという指定は、エラー文の中に記述してあります。↓
To avoid mistakes, drop_table is only reversible if given options or a block (can be empty).

解決策

マイグレーションファイルをブロック形式で書く必要があります。テーブルを使う予定がないので、カラムと型は指定しません。

20220404233925_masters.rb
class Masters < ActiveRecord::Migration[6.0]
  def change
    drop_table :masters
  end
end

↓少し変えただけです。

20220404233925_masters.rb
class Masters < ActiveRecord::Migration[6.0]
  def change
    drop_table :masters do
    end
  end
end

結果

# rails db:migrate:status 
database: movie_review_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20220404025856  Devise create admin users
   up     20220404025949  Create active admin comments
   up     20220404054303  Create movies
   up     20220404125722  Create users
   up     20220404130523  Create genres
  down    20220404233925  Masters

無事にdownさせることができました。


※その後の # rm db/migrate/20220404233925_masters.rb でマイグレーションファイルを削除できました!

参考

開発環境
mac OS バージョン11.6
エディタ
VScode

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
What you can do with signing up
0