# How to set one more primary keys?

**URL:** <https://www.ruby-forum.com/t/how-to-set-one-more-primary-keys/80016>\
**Category:** Rails\
**Created:** [December 20, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/how-to-set-one-more-primary-keys/80016 "2006-12-20T21:39:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 20, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/how-to-set-one-more-primary-keys/80016/1 "2006-12-20T21:39:59Z")

</div>

Now, I want to create table’s columns by “rake migrate”, but the  
default primary key is only ID. how to set one more primary keys?

saying, add same table structure like this sql:

CREATE TABLE samples (  
id INTEGER NOT NULL AUTO\_INCREMENT  
, code1 VARCHAR(3) NOT NULL  
, code2 VARCHAR(2) NOT NULL  
, name1 VARCHAR(20) NOT NULL  
, name2 VARCHAR(20) NOT NULL  
, PRIMARY KEY (id, code1, code2)  
);

Thanks,

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 20, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/how-to-set-one-more-primary-keys/80016/2 "2006-12-20T22:02:04Z")

</div>

Assuming you only want a primary key for uniqueness of that column  
(which would also allow you to grab that row by that primary key), you  
would add

class Sample \< ActiveRecord::Base

validates\_uniqueness\_of code1  
validates\_uniqueness\_of code2

before\_save :set\_codes

def set\_codes  
# Insert primary key code generation script here.  
end

def Sample.find\_by\_code\_one(id)  
Sample.find(:first, :conditions =\> [“code1 = ?”, id])  
end

def Sample.find\_by\_code\_two(id)  
Sample.find(:first, :conditions =\> [“code2 = ?”, id])  
end  
end

Why would you need two primary keys?

david wrote:

> Now, I want to create table’s columns by “rake migrate”, but the  
> default primary key is only ID. how to set one more primary keys?
> 
> saying, add same table structure like this sql:
> 
> CREATE TABLE samples (  
> id INTEGER NOT NULL AUTO\_INCREMENT  
> , code1 VARCHAR(3) NOT NULL  
> , code2 VARCHAR(2) NOT NULL  
> , name1 VARCHAR(20) NOT NULL  
> , name2 VARCHAR(20) NOT NULL  
> , PRIMARY KEY (id, code1, code2)  
> );
> 
> Thanks,

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 21, 2006, 2:59am UTC](https://www.ruby-forum.com/t/how-to-set-one-more-primary-keys/80016/3 "2006-12-21T02:59:12Z")

</div>

Thanks for your help. But what I mean is how to set multiple primary  
keys when table creation, maybe I’m not describe very clearly last  
time. The following is sample:

class CreateSamples \< ActiveRecord::Migration  
def self.up  
create\_table :samples do |t|  
t.column :code1, :string, :limit =\> 3, :default =\> “”, :null =\>  
false  
t.column :code2, :string, :limit =\> 2, :default =\> “”, :null =\>  
false  
t.column :name1, :string, :limit =\> 20, :default =\> “”, :null =\>  
false  
end  
end

def self.down  
drop\_table :t\_samples  
end  
end

As you know, the default primary key is “ID”, but I want to set “code1”  
and “code2” to primary key too. Would you have good idea?

about “why?”, because now I just try to migrate a old project to Rails.  
🙂

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 21, 2006, 4:35am UTC](https://www.ruby-forum.com/t/how-to-set-one-more-primary-keys/80016/4 "2006-12-21T04:35:21Z")

</div>

So what you want is the ability to set the _combination_ of the fields  
to be the primary key?

In this case the above code will not help you as there can be many  
equal code1/code2 values, but their combinations should be unique.  
This will always be the case if ID is included in the key as it is auto  
incrementing.

Try:  
add\_index(:samples, [:code1, :code2, :id], :unique =\> true)

and/or

add\_index(:samples, [:code1, :code2], :unique =\> true)

—ABS
