如何使用 Rails 迁移删除列

发布于 2024-09-01 15:46:40 字数 125 浏览 13 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(24

等风来 2024-09-08 15:46:40
remove_column :table_name, :column_name

例如:

remove_column :users, :hobby

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

remove_column :table_name, :column_name

For instance:

remove_column :users, :hobby

would remove the hobby Column from the users table.

柏林苍穹下 2024-09-08 15:46:40

对于旧版本的 Rails

ruby script/generate migration RemoveFieldNameFromTableName field_name:datatype

For Rails 3 及更高版本

rails generate migration RemoveFieldNameFromTableName field_name:datatype

For older versions of Rails

ruby script/generate migration RemoveFieldNameFromTableName field_name:datatype

For Rails 3 and up

rails generate migration RemoveFieldNameFromTableName field_name:datatype
≈。彩虹 2024-09-08 15:46:40

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

Rails 3 警告

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

rails generate migration RemoveFieldNameFromTableName field_name:datatype

生成的迁移将如下所示:

  def up
    remove_column :table_name, :field_name
  end

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

确保在从其中删除列时不要使用更改方法数据库表(Rails 3 应用程序中的迁移文件中不希望包含的内容的示例):

  def change
    remove_column :table_name, :field_name
  end

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

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 3 Warning

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):

  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.

把回忆走一遍 2024-09-08 15:46:40

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

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

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 .

class RemoveFieldFromTableName < ActiveRecord::Migration
  def change
    remove_column :table_name, :field_name, :data_type, {}
  end
end
残龙傲雪 2024-09-08 15:46:40

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

remove_column

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

remove_column :users, :first_name

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

你也可以使用change_table块来做到这一点,就像这样:

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

更喜欢这个,因为我发现它更清晰,而且你可以一次进行多项更改。

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

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

There are two good ways to do this:

remove_column

You can simply use remove_column, like so:

remove_column :users, :first_name

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

change_table block

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

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:

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

拥有 2024-09-08 15:46:40

清除& Rails 5 和 Rails 的简单说明6

  • 警告:您将丢失数据
  • 警告:以下说明适用于简单的迁移。对于具有数百万行、读/写数据库、集群等复杂迁移,此建议不适合您:

1. 创建迁移

在终端中运行以下命令:

railsgeneratemigrationremove_fieldname_from_tablename fieldname:fieldtype (按照惯例,表名称采用复数形式。请参阅此处的文档. )

示例: rails g migrationRemoveAcceptedFromQuotesaccepted:boolean

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. 运行迁移

rake db:migraterails db:migrate (它们都是相同的)

......然后你就可以开始比赛了!

Clear & Simple Instructions for Rails 5 & 6

  • WARNING: You will lose data.
  • Warning: the below instructions are for trivial migrations. For complex migrations with e.g. millions of rows, read/write dbs, clusters, this advice is not for you:

1. Create a migration

Run the following command in your terminal:

rails generate migration remove_fieldname_from_tablename fieldname:fieldtype (Table name in plural, as per convention. See the documentation here. )

Example: rails g migration RemoveAcceptedFromQuotes accepted:boolean

2. Check the migration

# 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

rake db:migrate or rails db:migrate (they're both the same)

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

屌丝范 2024-09-08 15:46:40

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

语法:

remove_column :table_name, :column_name, :type

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

示例:

remove_column :users, :last_name, :string

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

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 back if this migration is rollbacked (rake db:rollback).

The syntax:

remove_column :table_name, :column_name, :type

Removes column, also adds column back if 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.

骷髅 2024-09-08 15:46:40

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

rails generate migration remove_COLUMNNAME_from_TABLENAME COLUMNNAME:DATATYPE

例如从表 users 中删除列 access_level(string):

rails generate migration remove_access_level_from_users access_level:string

然后运行:

rake db:migrate

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

rails generate migration remove_COLUMNNAME_from_TABLENAME COLUMNNAME:DATATYPE

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

rails generate migration remove_access_level_from_users access_level:string

and then run:

rake db:migrate
水晶透心 2024-09-08 15:46:40

删除 RAILS 5 应用程序的列

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

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

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

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

Remove Columns For RAILS 5 App

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

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

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.

橘虞初梦 2024-09-08 15:46:40

您可以尝试以下操作:(

remove_column :table_name, :column_name

官方文档

You can try the following:

remove_column :table_name, :column_name

(Official documentation)

可是我不能没有你 2024-09-08 15:46:40
rails g migration RemoveXColumnFromY column_name:data_type

X = 列名称
Y = 表名称

EDIT

根据注释将 RemoveXColumnToY 更改为 RemoveXColumnFromY - 更清楚地了解迁移实际执行的操作。

rails g migration RemoveXColumnFromY column_name:data_type

X = column name
Y = table name

EDIT

Changed RemoveXColumnToY to RemoveXColumnFromY as per comments - provides more clarity for what the migration is actually doing.

相对绾红妆 2024-09-08 15:46:40

要从表中删除列,您必须运行以下迁移:

rails g migration remove_column_name_from_table_name column_name:data_type

然后运行命令:

rake db:migrate

To remove the column from table you have to run following migration:

rails g migration remove_column_name_from_table_name column_name:data_type

Then run command:

rake db:migrate
壹場煙雨 2024-09-08 15:46:40

change 方法中的 remove_column 将帮助您从表中删除该列。

class RemoveColumn < ActiveRecord::Migration
  def change
    remove_column :table_name, :column_name, :data_type
  end
end

请访问此链接以获取完整参考:http://guides.rubyonrails.org/active_record_migrations.html

remove_column in change method will help you to delete the column from the table.

class RemoveColumn < ActiveRecord::Migration
  def change
    remove_column :table_name, :column_name, :data_type
  end
end

Go on this link for complete reference : http://guides.rubyonrails.org/active_record_migrations.html

寄与心 2024-09-08 15:46:40

只需简单 3 个步骤即可从表中删除列,如下所示:

  1. 编写此命令

rails g migration remove_column_from_table_name

在按此名称和时间戳创建的终端一文件 (remove_column from_table_name) 中运行此命令后,

。然后转到这个文件。

  1. 你必须写入的内部文件

    remove_column :table_name, :column_name

  2. 最后转到控制台然后执行

    rake db:migrate

For removing column from table in just easy 3 steps as follows:

  1. write this command

rails g migration remove_column_from_table_name

after running this command in terminal one file created by this name and time stamp (remove_column from_table_name).

Then go to this file.

  1. inside file you have to write

    remove_column :table_name, :column_name

  2. Finally go to the console and then do

    rake db:migrate

热鲨 2024-09-08 15:46:40

步骤1:创建迁移

  rails g migration remove_column_name_from_table

步骤2:更改文件迁移中的代码刚刚创建的

rails版本< 3

  def change
    remove_column :table_name, :column_name, :datatype
  end

Rails 版本 >= 3

  def change
    remove_column :table_name, :column_name
  end

第 3 步:迁移

rake db:migrate

Step 1: Create a migration

  rails g migration remove_column_name_from_table

Step 2: Change code in file migration just created

rails version < 3

  def change
    remove_column :table_name, :column_name, :datatype
  end

rails version >= 3

  def change
    remove_column :table_name, :column_name
  end

Step 3: Migrate

rake db:migrate
浊酒尽余欢 2024-09-08 15:46:40

给出以下命令,它将自行添加到迁移文件中

rails g migration RemoveColumnFromModel

运行上述命令后,您可以检查迁移文件remove_column代码必须自行添加

然后迁移数据库

rake db:migrate

Give below command it will add in migration file on its own

rails g migration RemoveColumnFromModel

After running above command you can check migration file remove_column code must be added there on its own

Then migrate the db

rake db:migrate
三人与歌 2024-09-08 15:46:40

这是 Rails 控制台的另一项

ActiveRecord::Migration.remove_column(:table_name, :column_name)

Heres one more from rails console

ActiveRecord::Migration.remove_column(:table_name, :column_name)

↘人皮目录ツ 2024-09-08 15:46:40

简单地说,您可以删除列

remove_column :table_name, :column_name

例如,

remove_column :posts, :comment

Simply, You can remove column

remove_column :table_name, :column_name

For Example,

remove_column :posts, :comment
过期情话 2024-09-08 15:46:40

首先尝试运行以下命令创建迁移文件:

rails g migration RemoveAgeFromUsers age:string

然后在项目的根目录上运行以下命令运行迁移:

rails db:migrate

first try to create a migration file running the command:

rails g migration RemoveAgeFromUsers age:string

and then on the root directory of the project run the migration running the command:

rails db:migrate
二智少女 2024-09-08 15:46:40
  1. 在模型中将该列标记为忽略
class MyModel < ApplicationRecord
  self.ignored_columns = ["my_field"]
end
  1. 生成迁移
$ bin/rails g migration DropMyFieldFromMyModel
  1. 编辑迁移
class DropMyFieldFromMyModel < ActiveRecord::Migration[6.1]
  def change
    safety_assured { remove_column :my_table, :my_field }
  end
end
  1. 运行迁移
$ bin/rails db:migrate
  1. Mark the column as ignored in the model
class MyModel < ApplicationRecord
  self.ignored_columns = ["my_field"]
end
  1. Generate a migration
$ bin/rails g migration DropMyFieldFromMyModel
  1. Edit the migration
class DropMyFieldFromMyModel < ActiveRecord::Migration[6.1]
  def change
    safety_assured { remove_column :my_table, :my_field }
  end
end
  1. Run the migration
$ bin/rails db:migrate
被你宠の有点坏 2024-09-08 15:46:40

通过
删除列:表名,:列名
在迁移文件中

您可以通过键入以下内容直接在 Rails 控制台中删除列:
ActiveRecord::Base.remove_column:table_name, :column_name

Through
remove_column :table_name, :column_name
in a migration file

You can remove a column directly in a rails console by typing:
ActiveRecord::Base.remove_column :table_name, :column_name

晨敛清荷 2024-09-08 15:46:40

这样做;

因此

如果

无论如何,最好也考虑一下停机时间,因为 ActiveRecord 在运行时缓存数据库列, 如果您删除一列,它可能会导致异常,直到您的应用程序重新启动。

参考:强迁移

Do like this;

rails g migration RemoveColumnNameFromTables column_name:type

I.e. rails g migration RemoveTitleFromPosts title:string

Anyway, Would be better to consider about downtime as well since the ActiveRecord caches database columns at runtime so if you drop a column, it might cause exceptions until your app reboots.

Ref: Strong migration

愁以何悠 2024-09-08 15:46:40

只需在 Rails 控制台中运行它

ActiveRecord::Base.connection.remove_column("table_name", :column_name, :its_data_type)

TableName.find_by_sql(“ALTER TABLE table_name DROP column_name”)

Just run this in the rails console

ActiveRecord::Base.connection.remove_column("table_name", :column_name, :its_data_type)

or

TableName.find_by_sql(“ALTER TABLE table_name DROP column_name”)
夏日落 2024-09-08 15:46:40

您可以使用 Rails 迁移命令

rails generate migration RemoveColumnNameFromTableName column_name:column_type

来迁移数据库:

rails db:migrate

you can use rails migration command

rails generate migration RemoveColumnNameFromTableName column_name:column_type

than you can migrate the database:

rails db:migrate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文