1

migration file:

class CreateMyRecords < ActiveRecord::Migration
  def change
    create_table my_records:, :primary_key =>:partner_id do |t|
      t.integer :partner_id, references: [:Partner, :partnerID]
      t.integer :client_id, references: [:Client, :id]
    end
  end
end

How can I make partner_id and client_id a composite primary key? thank u!

Ming Cheng
  • 109
  • 2
  • 8
  • You possibly don't need to, have a read of this similar question and see if the `:through` association described in the answer suits your needs: http://stackoverflow.com/questions/5074206/give-composite-primary-key-in-rails – Matt May 24 '13 at 09:32

2 Answers2

2

My colleague gave me his answer and I think is't right, thanks for Sachin R & Matt any way:

create_table :my_records, id: false do |t|
  t.integer :partner_id, references: [:Partner, :partnerID]
  t.integer :client_id, references: [:Client, :id]
end
Ming Cheng
  • 109
  • 2
  • 8
-1

You not need to add compoiste primary key on migration. You can define composite primary key columns on your model by using composite_primary_keys gem http://compositekeys.rubyforge.org/

Sachin R
  • 11,606
  • 10
  • 35
  • 40
  • 5
    but only adding a composite primary key on migration can make this composite key into database, isn't it? – Ming Cheng May 24 '13 at 10:07