82
66

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 5 years have passed since last update.

Railsでテーブルの削除

Last updated at Posted at 2018-09-10

#概要
うっかりタイポで間違ったテーブル作ってしまった時の対処法。

#手順
モデルの削除コマンドをして削除したが、schemaに間違った方もあったので消えていないのだと確信。

###マイグレーションファイルの作成

本当はrecordsとしたかったところをrocordsとしてしまいあとで気がついた。テーブルを削除するためのマイグレーションファイルを作成する。

$ rails generate migration rocords

###マイグレーションファイル編集
消したいテーブルを指定して、drop_table :rocordsと記述する。

class DeleteRocords < ActiveRecord::Migration[5.1]
  def change
  	drop_table :rocords
  end
end

下記のコマンドを叩くと、データベースから削除される。

$ rails db:migrate

これでrecordsとrocordsが混在することはなくなった。なにより紛らわしい。

#参考資料
Ruby on Railsでテーブル削除

82
66
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
82
66

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?