# Help me create primary\_key with ActiveRecord::Migration

**URL:** <https://www.ruby-forum.com/t/help-me-create-primary-key-with-activerecord-migration/134812>\
**Category:** Rails\
**Created:** [April 15, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/help-me-create-primary-key-with-activerecord-migration/134812 "2008-04-15T12:02:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sumo](https://www.ruby-forum.com/u/sumo)\
**Post date:** [April 15, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/help-me-create-primary-key-with-activerecord-migration/134812/1 "2008-04-15T12:02:44Z")

</div>

I have two tables books and categories.  
I create a join table books\_categories.  
Now how to create couple primary key(book\_id,category\_id)

class BooksCategories \< ActiveRecord::Migration  
def self.up  
create\_table :books\_categories,:id =\> false do |t|  
t.integer :book\_id  
t.integer :category\_id  
end  
end  
…  
end

---

<div class="post-metadata">

**Author:** ![sumo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sumo](https://www.ruby-forum.com/u/sumo)\
**Post date:** [April 16, 2008, 6:52am UTC](https://www.ruby-forum.com/t/help-me-create-primary-key-with-activerecord-migration/134812/2 "2008-04-16T06:52:46Z")

</div>

Tuan M. wrote:

> I have two tables books and categories.  
> I create a join table books\_categories.  
> Now how to create couple primary key(book\_id,category\_id)
> 
> class BooksCategories \< ActiveRecord::Migration  
> def self.up  
> create\_table :books\_categories,:id =\> false do |t|  
> t.integer :book\_id  
> t.integer :category\_id  
> end  
> end  
> …  
> end

I believe by default rails will create a primary key for you

---

<div class="post-metadata">

**Author:** ![sumo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sumo](https://www.ruby-forum.com/u/sumo)\
**Post date:** [April 16, 2008, 6:56am UTC](https://www.ruby-forum.com/t/help-me-create-primary-key-with-activerecord-migration/134812/3 "2008-04-16T06:56:28Z")

</div>

You are removing your “primary key” with the :id =\> false

It’s not technically a “primary key” but by default ActiveRecord will  
look at that column.

Hope that helps some.

---

<div class="post-metadata">

**Author:** ![sumo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sumo](https://www.ruby-forum.com/u/sumo)\
**Post date:** [April 16, 2008, 7:08am UTC](https://www.ruby-forum.com/t/help-me-create-primary-key-with-activerecord-migration/134812/4 "2008-04-16T07:08:11Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Bryan R. wrote:

| It’s not technically a “primary key” but by default ActiveRecord will  
| look at that column.

Actually:  
mysql\> show fields from projects;  
±------------±-------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±------------±-------------±-----±----±--------±---------------+  
| id | int(11) | NO | PRI | NULL | auto\_increment |  
| comissioned | datetime | YES | | NULL | |  
| due | datetime | YES | | NULL | |  
| name | varchar(255) | YES | | NULL | |  
| description | text | YES | | NULL | |  
| created\_at | datetime | YES | | NULL | |  
| updated\_at | datetime | YES | | NULL | |  
±------------±-------------±-----±----±--------±---------------+  
7 rows in set (0.01 sec)

The migration:

| cat db\migrate\001\_create\_projects.rb  
class CreateProjects \< ActiveRecord::Migration  
~ def self.up  
~ create\_table :projects do |t|  
~ t.datetime :comissioned  
~ t.datetime :due  
~ t.string :name  
~ t.text :description

~ t.timestamps  
~ end  
~ end

~ def self.down  
~ drop\_table :projects  
~ end  
end

| jruby -S rails-v  
Rails 2.0.2

And no, I didn’t create any keys anywhere. Way too early to do that at  
this point in time. 😉

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)

~ - You know you’ve been hacking too long when…  
…you discover that you’re balancing your checkbook in octal.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgFiZ8ACgkQbtAgaoJTgL/WnQCeMPRe37fBLjPVbtdZ+iDUSUKg  
9HMAn1xwKZ4r1VKmJf8b11aVM4HJO1qv  
=CHFo  
-----END PGP SIGNATURE-----
