将主键添加到Rails中的现有字段

时间:2014-03-05 19:10:56

标签: ruby-on-rails postgresql migration

曾几何时我创建了一个没有id的连接表:

create_table :products_stores, id: false do |t|
  t.belongs_to :app
  t.belongs_to :product
end

我后来想把它用于每个产品的位置:

rename_table :products_stores, :inventories
add_column :inventories, :position, :integer

错误地添加了一个主键:

add_column :inventories, :id, :integer, primary: true

而不是:

add_column :inventories, :id, :primary_key

如何解决此问题,或者换句话说,如何将主键添加到现有列?

2 个答案:

答案 0 :(得分:1)

我最终这样做了:

SELECT q.* FROM ( 
    SELECT ah.datestamp, ad.domain, ah.human_hits
    FROM `a_hits_hourly` ah
    INNER JOIN a_saved_domains ad ON ah.domain_id = ad.domain_id  
    WHERE ah.datestamp > 2016070000 AND ah.human_hits > 0
) q 
GROUP BY q.domain

答案 1 :(得分:0)

也许你可以试试

change_column :inventories, :id, :primary_key

您还可以尝试使用错误回滚迁移并在再次迁移之前修复它。

相关问题