在 Rails 迁移 (MySQL) 中,您能否指定新列的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/371796/
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
In a Rails Migration (MySQL), can you specify what position a new column should be?
提问by yalestar
If I'm adding a column via MySQL, I can specify where in the table that column will be using the AFTER modifier. But if I do the add_column via a Rails migration, the column will be created at the end of the table.
如果我通过 MySQL 添加一列,我可以指定该列将在表中的哪个位置使用 AFTER 修饰符。但是,如果我通过 Rails 迁移执行 add_column,则将在表的末尾创建该列。
Is there any functionality for rails migrations to specify the position of an added column?
rails 迁移是否有任何功能来指定添加列的位置?
回答by Gabe Martin-Dempesy
This is now possible in Rails 2.3.6+ by passing the :after parameter
现在可以通过传递 :after 参数在 Rails 2.3.6+ 中实现
To everyone that doesn't see the advantage in having this feature: do you never look at your database outside of the ORM? If I'm viewing in any sort of UI, I like having things like foreign keys, status columns, flags, etc, all grouped together. This doesn't impact the application, but definitely speeds up my ability to review data.
对于没有看到此功能优势的每个人:您是否从未在 ORM 之外查看您的数据库?如果我在任何类型的 UI 中查看,我都喜欢将外键、状态列、标志等组合在一起。这不会影响应用程序,但肯定会加快我查看数据的能力。
回答by Tamik Soziev
Sure you can.
你当然可以。
Short answer:
add_column :users, :gender, :string, :after => :column_name
Long answer:
简短的回答:
add_column :users, :gender, :string, :after => :column_name
长答案:
Here is an example, let's say you want to add a column called "gender" after column "username" to "users" table.
这是一个示例,假设您要在“ username”列之后添加一个名为“ gender”的列到“ users”表。
- Type
rails g migration AddGenderToUser gender:string
Add "after => :username" in migration that was created so it looks like this:
class AddSlugToDictionary < ActiveRecord::Migration def change add_column :users, :gender, :string, :after => :username end end
- 类型
rails g migration AddGenderToUser gender:string
在创建的迁移中添加“ after => :username”,如下所示:
class AddSlugToDictionary < ActiveRecord::Migration def change add_column :users, :gender, :string, :after => :username end end
回答by Ben Marini
I created a patch that adds this additional functionality to the ActiveRecord Mysql adapter. It works for master and 2-3-stable.
我创建了一个补丁,将这个附加功能添加到 ActiveRecord Mysql 适配器。它适用于 master 和 2-3-stable。
It might be mysql specific, but it doesn't make your migrations any less portable (other adapters would just ignore the extra positioning options).
它可能是特定于 mysql 的,但它不会使您的迁移降低可移植性(其他适配器只会忽略额外的定位选项)。
回答by Bill Karwin
There does not seem to be a position option for the add_column
method in migrations. But migrations support executing literal SQL. I'm no Rails developer, but something like the following:
add_column
迁移中的方法似乎没有位置选项。但是迁移支持执行文字 SQL。我不是 Rails 开发人员,但类似于以下内容:
class AddColumnAfterOtherColumn < ActiveRecord::Migration
def self.up
execute "ALTER TABLE table_name ADD COLUMN column_name INTEGER
AFTER other_column"
end
def self.down
remove_column :table_name, :column_name
end
end
回答by Mike Woodhouse
There is no way within Rails to specify the position of a column. In fact, I think it's only coincidental (and therefore not to be relied on) that columns are created in the order they are named in a migration.
Rails 中无法指定列的位置。事实上,我认为列是按照它们在迁移中命名的顺序创建的,这只是巧合(因此不能依赖)。
The order of columns within a table is almost relevant and should be so: the common "reason" given is to be able to see a particular subset when executing a "SELECT *", but that's really not a good reason.
表中列的顺序几乎是相关的,应该是这样:给出的常见“原因”是在执行“SELECT *”时能够看到特定的子集,但这确实不是一个很好的理由。
Any other reason is probably a design smell, but I'd love to know a valid reason why I'm wrong!
任何其他原因都可能是设计气味,但我很想知道我错了的正当理由!
On some platforms, there is a (miniscule) space and performance saving to be obtained by putting the columns with the highest probability of being NULL to the end (because the DMBS will not use any disk space for "trailing" NULL values, but I think you'd have to be running on 1980's hardware to notice.
在某些平台上,通过将最有可能为 NULL 的列放在最后(因为 DMBS 不会将任何磁盘空间用于“尾随”NULL 值,但我认为您必须在 1980 年的硬件上运行才能注意到。