Ming Cheng
Ming Cheng

Reputation: 119

How can I set a composite primary key in my migration file in Ruby on Rails?

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!

Upvotes: 1

Views: 2546

Answers (2)

Ming Cheng
Ming Cheng

Reputation: 119

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

Upvotes: 2

Sachin R
Sachin R

Reputation: 11876

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/

Upvotes: -1

Related Questions