Adding a composite primary key in migration using raw sql

时间:2016-02-03 03:37:46

标签: mysql ruby-on-rails

I want to add a composite primary key into a table. So I added the following raw sql query into my migration file. However, when I run rake db:setup, I did not see a primary key for the table because schema.rb was not affected by this migration. How should I make this work? Thanks.

class AddPrimaryKeyToTable < ActiveRecord::Migration
  def up
    execute "ALTER TABLE table ADD PRIMARY KEY(identifier,time)"
  end

  ...

1 个答案:

答案 0 :(得分:0)

将sql查询更新到下面。

execute "ALTER TABLE table ADD PRIMARY KEY (identifier,time);"