552

我添加了一个我认为我会需要的表,但现在不再打算使用它。我应该如何删除该表?

我已经运行了迁移,所以该表在我的数据库中。我rails generate migration认为应该能够处理这个问题,但我还没有弄清楚怎么做。

我试过了:

rails generate migration drop_tablename

但这只是产生了一个空迁移。

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

4

24 回答 24

692

您并不总是能够简单地生成迁移以拥有您想要的代码。您可以创建一个空迁移,然后使用您需要的代码填充它。

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

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

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

drop_table :table_name
于 2010-10-26T01:54:46.897 回答
378

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

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 :productsand raise ActiveRecord::IrreversibleMigration

然后运行rake db:migrate,它会为你放下桌子。

于 2011-10-15T18:57:14.957 回答
363

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

至于迁移的代码,我只是引用 Maxwell Holder 的帖子Rails Migration Checklist

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

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

好 - 表明迁移不可逆的意图

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
于 2015-07-27T15:25:02.037 回答
209

警告:这样做需要您自担风险,正如@ z-atef@nzifnab正确指出的那样,Rails 不会意识到这些更改,您的迁移序列填充失败并且您的架构将与您的同事不同。这只是作为本地修补开发的资源。


虽然此处提供的答案正常工作,但我想要一些更“直截了当”的东西,我在这里找到了它:链接 首先进入 rails 控制台:

$rails console

然后只需键入:

ActiveRecord::Migration.drop_table(:table_name)

完成,为我工作!

于 2012-11-08T22:38:02.363 回答
38

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

rails generate migration drop_table_xyz

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

drop_table :tablename

或者,如果您想在不迁移的情况下删除表,只需通过以下方式打开 rails 控制台

$ rails c

并执行以下命令

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

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

ActiveRecord::Migration.drop_table(:table_name)
于 2015-05-26T07:42:53.620 回答
23
  1. rails g 迁移 drop_users
  2. 编辑迁移
    class DropUsers < ActiveRecord::Migration
      def change
        drop_table :users do |t|
          t.string :name
          t.timestamps
        end
      end
    end
  1. 耙分贝:迁移
于 2015-07-31T14:20:13.590 回答
15

简单而正式的方法是这样的:

  rails g migration drop_tablename

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

    def change
      drop_table :table_name
    end

然后你需要运行

    rake db:migrate 

在您的控制台上。

于 2017-07-28T06:57:26.470 回答
15

我无法使其与迁移脚本一起使用,因此我继续使用此解决方案。使用终端进入 rails 控制台:

rails c

类型

ActiveRecord::Migration.drop_table(:tablename)

这对我来说很有用。这将删除以前的表。别忘了跑

rails db:migrate
于 2018-12-11T12:26:58.437 回答
14

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

输入代码来创建一个您知道不再需要的表似乎有点奇怪,但这会使所有迁移代码保持完整和“官方”,对吗?

我只是为了一张我需要放下的桌子这样做,但老实说没有测试“向下”并且不知道为什么我会这样做。

于 2011-04-03T22:50:40.927 回答
12

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

def change
  drop_table(:users, force: true) if ActiveRecord::Base.connection.tables.include?('users')
end
于 2017-11-20T16:15:28.750 回答
12

您可以简单地从 rails 控制台删除一个表。首先打开控制台

$ rails c

然后将此命令粘贴到控制台

ActiveRecord::Migration.drop_table(:table_name)

table_name替换为要删除的表。

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

$ rails runner "Util::Table.clobber 'table_name'"
于 2015-10-16T06:59:32.473 回答
11

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

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

通过这种方式,您还可以回滚并用于恢复任何迁移

于 2017-05-05T17:42:34.887 回答
7

打开你的 Rails 控制台

ActiveRecord::Base.connection.execute("drop table table_name")
于 2012-12-14T06:41:05.653 回答
5

ActiveRecord::Base.connection.drop_table :table_name

于 2014-09-08T22:16:08.463 回答
3

如果有人正在寻找如何在 SQL 中执行此操作。

rails dbconsole从终端输入

输入密码

在控制台做

USE db_name;

DROP TABLE table_name;

exit

请不要忘记从架构中删除迁移文件和表结构

于 2019-07-18T06:58:05.867 回答
2

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

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"
于 2013-02-13T20:55:07.963 回答
2

您不能简单地运行drop_table :table_name,而是可以通过运行创建一个空迁移: rails g migration DropInstalls

然后,您可以将其添加到该空迁移中:

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

然后rails db:migrate在应该删除安装表的命令行中运行解决方案在这里找到

于 2021-11-13T08:22:42.260 回答
1

rake db:migrate:down VERSION=<version>

<version>您要还原的迁移文件的版本号在哪里。

例子:-

rake db:migrate:down VERSION=3846656238
于 2015-08-03T12:00:33.627 回答
1

Helpful documentation

在迁移中,您可以通过以下方式删除表:

drop_table(table_name, **options)

选项:

:force 设置为 :cascade 也可以删除依赖对象。默认为假

:if_exists 设置为 true 以仅在表存在时删除表。默认为假

例子:

  1. 为删除表创建迁移,例如我们要删除User

    rails g migration DropUsers
    
    Running via Spring preloader in process 13189
       invoke  active_record
       create    db/migrate/20211110174028_drop_users.rb
    
  2. 编辑迁移文件,在我们的例子中是db/migrate/20211110174028_drop_users.rb

    class DropUsers < ActiveRecord::Migration[6.1]
      def change
        drop_table :users, if_exist: true
      end
    end
    
  3. 运行迁移以删除User

    rails db:migrate
    
    == 20211110174028 DropUsers: migrating ===============================
    -- drop_table(:users, {:if_exist=>true})
       -> 0.4607s
    
于 2021-11-10T18:04:38.187 回答
1

你能做的最好的方法是

rails g migration Drop_table_Users

然后执行以下操作

rake db:migrate
于 2015-09-14T12:52:03.120 回答
-1

删除表/迁移

运行:- $ rails 生成迁移 DropTablename

exp:- $ rails 生成迁移 DropProducts

于 2018-04-20T14:54:46.103 回答
-1

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

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

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

$rails db:drop
于 2018-06-02T22:17:17.303 回答
-1

运行此命令:-

rails g migration drop_table_name

然后:

rake db:migrate

或者如果您使用的是 MySql 数据库,那么:

  1. 使用数据库登录
  2. show databases;
  3. show tables;
  4. drop table_name;
于 2016-06-17T13:21:47.697 回答
-4

如果要从架构中删除表,请执行以下操作 -

rails db:rollback
于 2019-03-04T12:42:03.113 回答