Deleting table from schema - Rails

Bitwise picture Bitwise · Aug 2, 2016 · Viewed 29.6k times · Source

I want to delete a table in my schema. I created the database when I first started the project and want the table removed. What is the best way of doing this?

I tried rails g migration drop table :installs but that just creates a empty migration?

Schema:

create_table "installs", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

add_index "installs", ["email"], name: "index_installs_on_email", unique: true
add_index "installs", ["reset_password_token"], name: "index_installs_on_reset_password_token", unique: true

Answer

Luka Kerr picture Luka Kerr · Aug 2, 2016

If you create an empty migration by running:
rails g migration DropInstalls
or:
rails generate migration DropInstalls

You can then add this into that empty migration:

class DropInstalls < ActiveRecord::Migration
  def change
    drop_table :installs
  end
end

Then run rake db:migrate in the command line which should remove the Installs table

Note: db/schema.rb is an autogenerated file. To alter the db structure, you should use the migrations instead of attempting to edit the schema file.