Ruby-on-rails 如何使用 Rails 迁移删除列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2831059/
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 22:40:23  来源:igfitidea点击:

How to drop columns using Rails migration

ruby-on-railsrubydatabaseactiverecordrails-migrations

提问by Ethan

What's the syntax for dropping a database table column through a Rails migration?

通过 Rails 迁移删除数据库表列的语法是什么?

回答by Nick Hammond

remove_column :table_name, :column_name

For instance:

例如:

remove_column :users, :hobby

would remove the hobby Column from the users table.

将从用户表中删除爱好列。

回答by prabu

For older versions of Rails

对于旧版本的 Rails

ruby script/generate migration RemoveFieldNameFromTableName field_name:datatype

For Rails 3 and up

对于 Rails 3 及更高版本

rails generate migration RemoveFieldNameFromTableName field_name:datatype

回答by Powers

Rails 4 has been updated, so the change method can be used in the migration to drop a column and the migration will successfully rollback. Please read the following warning for Rails 3 applications:

Rails 4 已经更新,所以迁移中可以使用change 方法删除列,迁移将成功回滚。请阅读以下有关 Rails 3 应用程序的警告:

Rails 3 Warning

Rails 3 警告

Please note that when you use this command:

请注意,当您使用此命令时:

rails generate migration RemoveFieldNameFromTableName field_name:datatype

The generated migration will look something like this:

生成的迁移看起来像这样:

  def up
    remove_column :table_name, :field_name
  end

  def down
    add_column :table_name, :field_name, :datatype
  end

Make sure to not use the change method when removing columns from a database table (example of what you don't want in the migration file in Rails 3 apps):

从数据库表中删除列时,请确保不要使用更改方法(Rails 3 应用程序中的迁移文件中不需要的示例):

  def change
    remove_column :table_name, :field_name
  end

The change method in Rails 3 is not smart when it comes to remove_column, so you will not be able to rollback this migration.

Rails 3 中的 change 方法在涉及 remove_column 时并不智能,因此您将无法回滚此迁移。

回答by Lars Schirrmeister

In a rails4 app it is possible to use the change method also for removing columns. The third param is the data_type and in the optional forth you can give options. It is a bit hidden in the section 'Available transformations' on the documentation.

在 rails4 应用程序中,也可以使用更改方法来删除列。第三个参数是 data_type,在可选的第四个参数中,您可以提供选项。它有点隐藏在文档的“可用转换”部分中。

class RemoveFieldFromTableName < ActiveRecord::Migration
  def change
    remove_column :table_name, :field_name, :data_type, {}
  end
end

回答by superluminary

There are two good ways to do this:

有两种好方法可以做到这一点:

remove_column

删除列

You can simply use remove_column, like so:

您可以简单地使用 remove_column,如下所示:

remove_column :users, :first_name

This is fine if you only need to make a single change to your schema.

如果您只需要对架构进行一次更改,这很好。

change_table block

change_table 块

You can also do this using a change_table block, like so:

您也可以使用 change_table 块执行此操作,如下所示:

change_table :users do |t|
  t.remove :first_name
end

I prefer this as I find it more legible, and you can make several changes at once.

我更喜欢这个,因为我觉得它更易读,而且您可以一次进行多次更改。

Here's the full list of supported change_table methods:

以下是受支持的 change_table 方法的完整列表:

http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table

http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table

回答by aschmid

in rails 5 you can use this command in the terminal:

在 rails 5 中,您可以在终端中使用此命令:

rails generate migration remove_COLUMNNAME_from_TABLENAME COLUMNNAME:DATATYPE

for example to remove the column access_level(string) from table users:

例如,从表用户中删除列 access_level(string):

rails generate migration remove_access_level_from_users access_level:string

and then run:

然后运行:

rake db:migrate

回答by Imran Ahmad

Generate a migration to remove a column such that if it is migrated (rake db:migrate), it should drop the column. And it should add column backif this migration is rollbacked (rake db:rollback).

生成迁移以删除列,如果它被迁移 ( rake db:migrate),它应该删除列。如果此迁移被回滚 ( ) ,它应该重新添加列rake db:rollback

The syntax:

语法:

remove_column :table_name, :column_name, :type

remove_column :table_name, :column_name, :type

Removes column, also adds column backif migration is rollbacked.

删除列,如果迁移被回滚,也会重新添加列

Example:

例子:

remove_column :users, :last_name, :string

Note: If you skip the data_type, the migration will remove the column successfully but if you rollback the migration it will throw an error.

注意如果跳过 data_type,迁移将成功删除列,但如果回滚迁移,则会引发错误。

回答by BKSpurgeon

Clear & Simple Instructions for Rails 5.2

Rails 5.2 清晰简单的说明

  • WARNING: You will lose data if you remove a column from your database. To proceed, see below:
  • Warning: the below instructions are for trivial migrations. For complex migrations with e.g. millions and millions of rows, you will have to account for the possibility of failures, you will also have to think about how to optimise your migrations so that they run swiftly, and the possibility that users will use your app while the migration process is occurring. If you have multiple databases, or if anything is remotely complicated, then don't blame me if anything goes wrong!
  • 警告:如果从数据库中删除列,您将丢失数据。要继续,请参见下文:
  • 警告:以下说明适用于琐碎的迁移。对于包含数百万行的复杂迁移,您必须考虑失败的可能性,您还必须考虑如何优化迁移以使其快速运行,以及用户在使用您的应用程序时使用您的应用程序的可能性迁移过程正在发生。如果您有多个数据库,或者任何事情都很复杂,那么如果出现任何问题,请不要怪我!


1. Create a migration

1. 创建迁移

Runthe following command in your terminal:

在终端中运行以下命令:

rails generate migration remove_fieldname_from_tablename fieldname:fieldtype

rails generate migration remove_fieldname_from_tablename fieldname:fieldtype

Note: the table name should be in plural form as per rails convention.

注意:按照 rails 约定,表名应该是复数形式。

Example:

例子:

In my case I want to remove the acceptedcolumn (a boolean value) from the quotestable:

在我的情况下,我想acceptedquotes表中删除列(布尔值):

rails g migration RemoveAcceptedFromQuotes accepted:boolean

rails g migration RemoveAcceptedFromQuotes accepted:boolean

See the documentationre: a convention when adding/removing fields to a table:

请参阅文档:向表中添加/删除字段时的约定:

There is a special syntactic shortcut to generate migrations that add fields to a table.

rails generate migration add_fieldname_to_tablename fieldname:fieldtype

有一个特殊的语法快捷方式来生成将字段添加到表的迁移。

rails 生成迁移 add_fieldname_to_tablename fieldname:fieldtype

2. Check the migration

2. 检查迁移

# db/migrate/20190122035000_remove_accepted_from_quotes.rb
class RemoveAcceptedFromQuotes < ActiveRecord::Migration[5.2]
  # with rails 5.2 you don't need to add a separate "up" and "down" method.
  def change
    remove_column :quotes, :accepted, :boolean
  end
end

3. Run the migration

3. 运行迁移

rake db:migrate

rake db:migrate

....And then you're off to the races!

....然后你就要去比赛了!

回答by Lorem Ipsum Dolor

Remove Columns For RAILS 5 App

删除 RAILS 5 应用程序的列

rails g migration Remove<Anything>From<TableName> [columnName:type]

Command above generate a migration file inside db/migratedirectory. Snippet blow is one of remove column from table example generated by Rails generator,

上面的命令在db/migrate目录中生成一个迁移文件。片段打击是从 Rails 生成器生成的表示例中删除列之一,

class RemoveAgeFromUsers < ActiveRecord::Migration
  def up
    remove_column :users, :age
  end
  def down
    add_column :users, :age, :integer
  end
end

I also made a quick reference guide for Rails which can be found at here.

我还制作了 Rails 的快速参考指南,可在此处找到。

回答by Jordan Running

You can try the following:

您可以尝试以下操作:

remove_column :table_name, :column_name

(Official documentation)

官方文档