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

问题描述 投票:466回答:21

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

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

我试过了:

rails generate migration drop_tablename

但那只是一个空的迁移。

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

ruby-on-rails database ruby-on-rails-3 migration rake
21个回答
613
投票

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

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

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

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

drop_table :table_name

7
投票

打开你的rails控制台

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

7
投票

简单而官方的方式是:

  rails g migration drop_tablename

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

    def change
      drop_table :table_name
    end

然后你需要运行

    rake db:migrate 

在你的控制台上。


5
投票

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

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

4
投票

ActiveRecord::Base.connection.drop_table :table_name


3
投票

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

rails c

类型

ActiveRecord::Migration.drop_table(:tablename)

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

rails db:migrate

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"

1
投票

你能做的最好的方法是

rails g migration Drop_table_Users

然后执行以下操作

rake db:migrate

1
投票

rake db:migrate:down VERSION=<version>

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

例:-

rake db:migrate:down VERSION=3846656238

0
投票

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

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

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

$rails db:drop

0
投票

运行此命令: -

rails g migration drop_table_name

然后:

rake db:migrate

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

  1. 用数据库登录
  2. show databases;
  3. show tables;
  4. drop table_name;

340
投票

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

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,它会为你丢桌子。


0
投票

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

rails db:rollback

0
投票

如果有人在SQL中寻找如何做到这一点。

从终端输入rails dbconsole

输入密码

在控制台做

USE db_name;

DROP TABLE table_name;

exit

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


-1
投票

删除表/迁移

运行: - $ rails生成迁移DropTablename

exp: - $ rails生成迁移DropProducts


295
投票

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

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

BAD - running rake db:migrate and then rake db:rollback will fail

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

GOOD - reveals intent that migration should not be reversible

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

  def down
    fail ActiveRecord::IrreversibleMigration
  end
end

BETTER - is actually reversible

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

191
投票

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

$rails console

然后输入:

ActiveRecord::Migration.drop_table(:table_name)

完成了,为我工作!


35
投票

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

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)

21
投票
  1. rails g migration drop_users
  2. 编辑迁移
    class DropUsers < ActiveRecord::Migration
      def change
        drop_table :users do |t|
          t.string :name
          t.timestamps
        end
      end
    end
  1. rake db:migrate

13
投票

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

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

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


12
投票

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

$ rails c

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

ActiveRecord::Migration.drop_table(:table_name)

将table_name替换为要删除的表。

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

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

8
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.