Rails : Create a drop table cascade migration

前端 未结 3 2041
滥情空心
滥情空心 2021-02-18 18:52

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\")?

相关标签:
3条回答
  • 2021-02-18 19:23

    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
    
    0 讨论(0)
  • 2021-02-18 19:28

    In Rails 4 you can do the following:

    drop_table :accounts, force: :cascade
    
    0 讨论(0)
  • 2021-02-18 19:34

    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.

    0 讨论(0)
提交回复
热议问题