0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Railsで複数のテーブルを一括削除する

Posted at

Railsで昔に作ったテーブルが複数残ってしまっていたので、
新しいテーブルを作り、migrateした際に、
already exists(既にあるよ)と怒られていた。
たまに削除したくなったりするので、忘れないように投稿します(o'∀')ノ

流れ

① 空のmigrationファイルを作成

$ rails g migration destroy(destroyは任意の名前)

② 出来上がったmigrationファイルに

class Destroy < ActiveRecord::Migration[6.1]
  def change
    drop_table :不要テーブル名
    drop_table :不要テーブル名
    drop_table :不要テーブル名
  end
end

③ migrateする

$ rails db:migrate

これで不要なテーブルは削除されます。
+αでモデルの削除もした方がよろしいと思いました。

参考資料

Railsでテーブルの削除

0
0
0

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
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?