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")?
SchemaDumper uses force: :cascade on create_table . This makes it possible to reload a schema when foreign keys are in place.
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.
In Rails 4 you can do the following:
drop_table :accounts, force: :cascade
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With