Primary keys and migrations


#1

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.


#2

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