Rails DB Migration - How To Drop a Table?

Viewed 446788

I added a table that I thought I was going to need, but now no longer plan on using it. How should I remove that table?

I've already run migrations, so the table is in my database. I figure rails generate migration should be able to handle this, but I haven't figured out how yet.

I've tried:

rails generate migration drop_tablename

but that just generated an empty migration.

What is the "official" way to drop a table in Rails?

24 Answers

I wasn't able to make it work with migration script so I went ahead with this solution. Enter rails console using the terminal:

rails c

Type

ActiveRecord::Migration.drop_table(:tablename)

It works well for me. This will remove the previous table. Don't forget to run

rails db:migrate

Alternative to raising exception or attempting to recreate a now empty table - while still enabling migration rollback, redo etc -

def change
  drop_table(:users, force: true) if ActiveRecord::Base.connection.tables.include?('users')
end

You can't simply run drop_table :table_name, instead you can create an empty migration by running: rails g migration DropInstalls

You can then add this into that empty migration:

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

Then run rails db:migrate in the command line which should remove the Installs table The solution was found here

if anybody is looking for how to do it in SQL.

type rails dbconsole from terminal

enter password

In console do

USE db_name;

DROP TABLE table_name;

exit

Please dont forget to remove the migration file and table structure from schema

Helpful documentation

In migration you can drop table by:

drop_table(table_name, **options)

options:

:force Set to :cascade to drop dependent objects as well. Defaults to false

:if_exists Set to true to only drop the table if it exists. Defaults to false

Example:

  1. Create migration for drop table, for example we are want to drop User table

    rails g migration DropUsers
    
    Running via Spring preloader in process 13189
       invoke  active_record
       create    db/migrate/20211110174028_drop_users.rb
    
  2. Edit migration file, in our case it is db/migrate/20211110174028_drop_users.rb

    class DropUsers < ActiveRecord::Migration[6.1]
      def change
        drop_table :users, if_exist: true
      end
    end
    
  3. Run migration for dropping User table

    rails db:migrate
    
    == 20211110174028 DropUsers: migrating ===============================
    -- drop_table(:users, {:if_exist=>true})
       -> 0.4607s
    

Drop Table/Migration

run:- $ rails generate migration DropTablename

exp:- $ rails generate migration DropProducts

if you want to drop a specific table you can do

$ rails db:migrate:up VERSION=[Here you can insert timestamp of table]

otherwise if you want to drop all your database you can do

$rails db:drop

If you want to delete the table from the schema perform below operation --

rails db:rollback
Related