# Primary keys and migrations

**URL:** <https://www.ruby-forum.com/t/primary-keys-and-migrations/57072>\
**Category:** Rails\
**Created:** [April 7, 2006, 12:13am UTC](https://www.ruby-forum.com/t/primary-keys-and-migrations/57072 "2006-04-07T00:13:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjlyders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rjlyders](https://www.ruby-forum.com/u/rjlyders)\
**Post date:** [April 7, 2006, 12:13am UTC](https://www.ruby-forum.com/t/primary-keys-and-migrations/57072/1 "2006-04-07T00:13:18Z")

</div>

I have a join table I’m creating with a migration as follows:

```
 create_table :read_global_messages, :id=>false do |t|
   t.column :user_id, :integer
   t.column :message_id, :integer
 end

```

So far so good, however I want the primary key to be user\_id and  
message\_id. How do I do this?

I tried the following, but that doesn’t work:

```
 add_index :read_global_messages, [:user_id, :message_id], :primary

```

I could just add it as a normal index, but ideally I’d like it to be a  
primary key otherwise this table will be without one (as there’s no ID  
column).

Any ideas?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rjlyders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rjlyders](https://www.ruby-forum.com/u/rjlyders)\
**Post date:** [April 7, 2006, 1:13am UTC](https://www.ruby-forum.com/t/primary-keys-and-migrations/57072/2 "2006-04-07T01:13:16Z")

</div>

Richard L. wrote:

> I have a join table I’m creating with a migration as follows:
> 
> ```
> create_table :read_global_messages, :id=>false do |t|
> t.column :user_id, :integer
> t.column :message_id, :integer
> end
> 
> ```
> 
> So far so good, however I want the primary key to be user\_id and  
> message\_id. How do I do this?

Rails doesn’t support either composite primary keys or primary keys on  
join tables. If you need a primary key in your table, you probably want  
to make it a full-fledged join model (has\_many :through instead of  
habtm).

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
