Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails : Create a drop table cascade migration

How do I force a DROP TABLE CASCADE in a Rails 3.2 migration?

Is there an option to pass to drop_table("table_name")?

like image 384
zabumba Avatar asked Jun 04 '13 22:06

zabumba


People also ask

What does force Cascade do rails?

SchemaDumper uses force: :cascade on create_table . This makes it possible to reload a schema when foreign keys are in place.

What is DROP TABLE cascade?

The CASCADE option allows you to remove the table and its dependent objects. The RESTRICT option rejects the removal if there is any object depends on the table. The RESTRICT option is the default if you don't explicitly specify it in the DROP TABLE statement.


3 Answers

In Rails 4 you can do the following:

drop_table :accounts, force: :cascade
like image 186
waratuman Avatar answered Oct 24 '22 12:10

waratuman


You could always run raw SQL in the migration.

MYSQL:

execute "DROP TABLE #{:table_name} CASCADE CONSTRAINTS PURGE"

PostgreSQL:

execute "DROP TABLE #{:table_name} CASCADE"

You can check the documentation of the built-in method drop_table here.

like image 10
jeremywoertink Avatar answered Oct 24 '22 11:10

jeremywoertink


Put a file in your initializers directory called postgres.rb then did. This works for rails 4.1 anyway.

module ActiveRecord
    module ConnectionAdapters # :nodoc:
        module SchemaStatements
            def drop_table(table_name, options = {})
                execute "DROP TABLE  #{quote_table_name(table_name)} CASCADE"
            end
        end
    end
end
like image 2
Cid Dennis Avatar answered Oct 24 '22 12:10

Cid Dennis