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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4020131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:28:58  来源:igfitidea点击:

Rails DB Migration - How To Drop a Table?

ruby-on-railsdatabaseruby-on-rails-3migrationrake

提问by Jason Whitehorn

I added a table that I thought I was going to need, but now no longer plan on using it. How should I remove that table?

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

I've already run migrations, so the table is in my database. I figure rails generate migrationshould be able to handle this, but I haven't figured out how yet.

我已经运行了迁移,所以该表在我的数据库中。我想rails generate migration应该能够处理这个,但我还没有想出如何。

I've tried:

我试过了:

rails generate migration drop_tablename

but that just generated an empty migration.

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

What is the "official" way to drop a table in Rails?

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

回答by Pete

You won't always be able to simply generate the migration to already have the code you want. You can create an empty migration and then populate it with the code you need.

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

You can find information about how to accomplish different tasks in a migration here:

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

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

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

More specifically, you can see how to drop a table using the following approach:

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

drop_table :table_name

回答by Brandon O'Rourke

First generate an empty migration with any name you'd like. It's important to do it this way since it creates the appropriate date.

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

rails generate migration DropProductsTable

This will generate a .rb file in /db/migrate/ like 20111015185025_drop_products_table.rb

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

Now edit that file to look like this:

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

class DropProductsTable < ActiveRecord::Migration
  def up
    drop_table :products
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end

The only thing I added was drop_table :productsand raise ActiveRecord::IrreversibleMigration.

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

Then run rake db:migrateand it'll drop the table for you.

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

回答by Beder Acosta Borges

Write your migration manually. E.g. run rails g migration DropUsers.

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

As for the code of the migration I'm just gonna quote Maxwell Holder's post Rails Migration Checklist

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

BAD - running rake db:migrateand then rake db:rollbackwill fail

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

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

回答by lllllll

While the answers provided here work properly, I wanted something a bit more 'straightforward', I found it here: linkFirst enter rails console:

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

$rails console

Then just type:

然后只需键入:

ActiveRecord::Migration.drop_table(:table_name)

And done, worked for me!

完成了,对我有用!

回答by Shahzad Tariq

You need to to create a new migration file using following command

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

rails generate migration drop_table_xyz

and write drop_table code in newly generated migration file (db/migration/xxxxxxx_drop_table_xyz) like

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

drop_table :tablename

Or if you wanted to drop table without migration, simply open rails console by

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

$ rails c

and execute following command

并执行以下命令

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

or you can use more simplified command

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

ActiveRecord::Migration.drop_table(:table_name)

回答by Aashish Saini

  1. rails g migration drop_users
  2. edit the migration
  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. rake db:migrate
  1. 耙数据库:迁移

回答by Francis Potter

I think, to be completely "official", you would need to create a new migration, and put drop_table in self.up. The self.down method should then contain all the code to recreate the table in full. Presumably that code could just be taken from schema.rb at the time you create the migration.

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

It seems a little odd, to put in code to create a table you know you aren't going to need anymore, but that would keep all the migration code complete and "official", right?

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

I just did this for a table I needed to drop, but honestly didn't test the "down" and not sure why I would.

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

回答by Farzpal Singh

you can simply drop a table from rails console. first open the console

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

$ rails c

then paste this command in console

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

ActiveRecord::Migration.drop_table(:table_name)

replace table_namewith the table you want to delete.

table_name替换为要删除的表。

you can also drop table directly from the terminal. just enter in the root directory of your application and run this command

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

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

回答by Mahesh Mesta

The simple and official way would be this:

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

  rails g migration drop_tablename

Now go to your db/migrate and look for your file which contains the drop_tablename as the filename and edit it to this.

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

    def change
      drop_table :table_name
    end

Then you need to run

然后你需要运行

    rake db:migrate 

on your console.

在您的控制台上。

回答by Matheus Silva

You can roll back a migration the way it is in the guide:

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

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

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

Generate a migration:

生成迁移:

rails generate migration revert_create_tablename

Write the migration:

编写迁移:

require_relative '20121212123456_create_tablename'

class RevertCreateTablename < ActiveRecord::Migration[5.0]
  def change
    revert CreateTablename    
  end
end

This way you can also rollback and can use to revert any migration

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