Forum: Ruby on Rails Primary keys and migrations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C004d67820a114e24ccf6f2ddaf5b236?d=identicon&s=25 Richard Livsey (Guest)
on 2006-04-07 00:13
(Received via mailing list)
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.
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-04-07 01:13
Richard Livsey 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 Susser
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.