假设我在 Rails 迁移中创建了一个表,指定省略 ID 列:
create_table :categories_posts, :id => false do |t|
t.column :category_id, :integer, :null => false
t.column :post_id, :integer, :null => false
end
后来我决定要添加一个 ID 列作为主键,所以我创建了一个新的迁移:
class ChangeCategoriesToRichJoin < ActiveRecord::Migration
def self.up
add_column :categories_posts, :id, :primary_key
end
def self.down
remove_column :categories_posts, :id
end
end
但是当我迁移后查看表格时,它看起来像这样:
category_id
post_id
id
id 列位于表中的最后一个位置,而通常 id 列将位于第一个位置。
有没有办法更改 ChangeCategoriesToRichJoin 迁移以坚持在表中的 category_id 列之前创建 id 列?
或者我是否需要删除表并在“创建表”定义中添加列?
最佳答案
使用 :after => :another_column_name
,例如:
change_table :users do |t|
t.integer :like_count, :default => 0, :after => :view_count
end
关于ruby-on-rails - 在 Rails 迁移中,是否可以指示新添加的列应该在表中的现有列之前还是之后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/880871/