Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting table from schema - Rails

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
like image 625
Bitwise Avatar asked Aug 02 '16 03:08

Bitwise


People also ask

How do you delete a table from schema?

DROP TABLE removes tables from the database. Only the table owner, the schema owner, and superuser can drop a table. To empty a table of rows without destroying the table, use DELETE or TRUNCATE . DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table.

How remove data from table in rails?

Rails delete operation using destroy method By using destroy, you can delete the record from rails as well as its other existing dependencies. So in the context of our rails application, if we delete a book record using the destroy function, the authors associated with the book will also be deleted.


1 Answers

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.

like image 191
Luka Kerr Avatar answered Sep 18 '22 14:09

Luka Kerr