首页 文章

Rails数据库迁移 - 如何删除表?

提问于
浏览
444

我添加了一张我认为我需要的表格,但现在不再计划使用它了 . 我该如何删除该表?

我已经运行了迁移,因此该表位于我的数据库中 . 我想 rails generate migration 应该能够处理这个,但我还没弄明白怎么回事 .

我试过了:

rails generate migration drop_tablename

但那只是生成了一个空的迁移 .

在Rails中删除表的“官方”方法是什么?

20 回答

  • 21
    • rails g migration drop_users

    • 编辑迁移

    class DropUsers < ActiveRecord::Migration
          def change
            drop_table :users do |t|
              t.string :name
              t.timestamps
            end
          end
        end
    
    • rake db:migrate
  • 2

    您无法始终只生成已迁移到已拥有所需代码的迁移 . 您可以创建一个空迁移,然后使用您需要的代码填充它 .

    您可以在此处找到有关如何在迁移中完成不同任务的信息:

    http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

    更具体地说,您可以使用以下方法查看如何删除表:

    drop_table :table_name
    
  • 179

    首先使用您想要的任何名称生成空迁移 . 这样做很重要,因为它创建了适当的日期 .

    rails generate migration DropProductsTable
    

    这将在/ db / migrate /中生成一个.rb文件,如20111015185025_drop_products_table.rb

    现在编辑该文件如下所示:

    class DropProductsTable < ActiveRecord::Migration
      def up
        drop_table :products
      end
    
      def down
        raise ActiveRecord::IrreversibleMigration
      end
    end
    

    我添加的唯一内容是 drop_table :productsraise ActiveRecord::IrreversibleMigration .

    然后运行 rake db:migrate ,它会为你删除表 .

  • 326

    手动编写迁移 . 例如 . 运行 rails g migration DropUsers .

    至于迁移的代码我'm just gonna quote Maxwell Holder'发表Rails Migration Checklist

    BAD - 运行rake db:migrate然后rake db:rollback将失败

    class DropUsers < ActiveRecord::Migration
      def change
        drop_table :users
      end
    end
    

    GOOD - 揭示迁移不应该是可逆的意图

    class DropUsers < ActiveRecord::Migration
      def up
        drop_table :users
      end
    
      def down
        fail ActiveRecord::IrreversibleMigration
      end
    end
    

    更好 - 实际上是可逆的

    class DropUsers < ActiveRecord::Migration
      def change
        drop_table :users do |t|
          t.string :email, null: false
          t.timestamps null: false
        end
      end
    end
    
  • 0

    虽然这里提供的答案工作正常,我想要更多的东西'straightforward',我在这里找到它:link首先进入rails console:

    $rails console
    

    然后输入:

    ActiveRecord::Migration.drop_table(:table_name)
    

    完成了,为我工作!

  • 1

    您需要使用以下命令创建新的迁移文件

    rails generate migration drop_table_xyz
    

    并在新生成的迁移文件(db / migration / xxxxxxx_drop_table_xyz)中编写drop_table代码

    drop_table :tablename
    

    或者,如果您想在不迁移的情况下删除表,只需打开rails console

    $ rails c
    

    并执行以下命令

    ActiveRecord::Base.connection.execute("drop table table_name")
    

    或者您可以使用更简化的命令

    ActiveRecord::Migration.drop_table(:table_name)
    
  • 580

    我认为,要完全“正式”,您需要创建一个新的迁移,并将drop_table放在self.up中 . 然后self.down方法应包含所有代码以完整地重新创建表 . 据推测,您可以在创建迁移时从schema.rb中获取代码 .

    看起来有点奇怪,要放入代码来创建一个你知道你不再需要的表,但这样可以保持所有的迁移代码完整并且“正式”,对吧?

    我只是为了一个我需要放弃的 table 做了这个,但老实说没有测试“向下”并且不确定为什么我会这样做 .

  • 3

    你可以简单地从rails控制台中删除一个表 . 首先打开控制台

    $ rails c
    

    然后在控制台中粘贴此命令

    ActiveRecord::Migration.drop_table(:table_name)
    

    table_name 替换为您要删除的表 .

    您也可以直接从终端删除表 . 只需输入应用程序的根目录并运行此命令即可

    $ rails runner "Util::Table.clobber 'table_name'"
    
  • 34

    打开你的rails控制台

    ActiveRecord::Base.connection.execute("drop table table_name")
    
  • 4

    您可以按照指南中的方式回滚迁移:

    http://guides.rubyonrails.org/active_record_migrations.html#reverting-previous-migrations

    生成迁移:

    rails generate migration revert_create_tablename
    

    写下迁移:

    require_relative '20121212123456_create_tablename'
    
    class RevertCreateTablename < ActiveRecord::Migration[5.0]
      def change
        revert CreateTablename    
      end
    end
    

    这样您也可以回滚并可以用来还原任何迁移

  • 13

    简单而官方的方式是:

    rails g migration drop_tablename
    

    现在转到db / migrate并查找包含drop_tablename作为文件名的文件,并将其编辑为此文件 .

    def change
          drop_table :table_name
        end
    

    然后你需要跑

    rake db:migrate
    

    在你的控制台上 .

  • 1

    ActiveRecord::Base.connection.drop_table :table_name

  • 2

    替代引发异常或尝试重新创建现在空表 - 同时仍然启用迁移回滚,重做等 -

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

    我需要删除我们的迁移脚本以及表格本身...

    class Util::Table < ActiveRecord::Migration
    
     def self.clobber(table_name)   
        # drop the table
        if ActiveRecord::Base.connection.table_exists? table_name
          puts "\n== " + table_name.upcase.cyan + " ! " 
               << Time.now.strftime("%H:%M:%S").yellow
          drop_table table_name 
        end
    
        # locate any existing migrations for a table and delete them
        base_folder = File.join(Rails.root.to_s, 'db', 'migrate')
        Dir[File.join(base_folder, '**', '*.rb')].each do |file|
          if file =~ /create_#{table_name}.rb/
            puts "== deleting migration: " + file.cyan + " ! "
                 << Time.now.strftime("%H:%M:%S").yellow
            FileUtils.rm_rf(file)
            break
          end
        end
      end
    
      def self.clobber_all
        # delete every table in the db, along with every corresponding migration 
        ActiveRecord::Base.connection.tables.each {|t| clobber t}
      end
    
    end
    

    从终端窗口运行:

    $ rails runner "Util::Table.clobber 'your_table_name'"
    

    要么

    $ rails runner "Util::Table.clobber_all"
    
  • 7

    Run this command:-

    rails g migration drop_table_name
    

    然后:

    rake db:migrate
    

    or if you are using MySql database then:

    • 使用数据库登录

    • show databases;

    • show tables;

    • drop table_name;

  • 10

    你能做的最好的方法是

    rails g migration Drop_table_Users
    

    然后执行以下操作

    rake db:migrate
    
  • -1

    rake db:migrate:down VERSION=<version>
    

    其中 <version> 是您要还原的迁移文件的版本号 .

    例:-

    rake db:migrate:down VERSION=3846656238
    
  • 7

    如果你想删除一个特定的表,你可以做

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

    否则,如果你想删除所有数据库,你可以做

    $rails db:drop
    
  • 0

    我无法使用迁移脚本,因此我继续使用此解决方案 . 使用终端输入rails console:

    rails c
    

    类型

    ActiveRecord::Migration.drop_table(:tablename)
    

    这对我来说很有用 . 这将删除前一个表 . 别忘了跑

    rails db:migrate
    
  • 268

    Drop Table/Migration

    运行: - $ rails生成迁移DropTablename

    exp: - $ rails生成迁移DropProducts

相关问题