how to set up multiple primary key in the rails migrate?

is there anyone who knows that?
i have searched the web and api,but not found. :frowning:
thanks.

Isn't a primary key unique by definition ?

Fred

i means primary_key(:tag_id,:book_id).like this.

maybe combination primary key is more clear than "multiple".

Vincent.Z wrote:

i means primary_key(:tag_id,:book_id).like this.

maybe combination primary key is more clear than "multiple".

On Apr 9, 10:56�pm, Frederick Cheung <frederick.che...@gmail.com>

This is called a composite primary key and there are plugins/gems to
handle this. Search for composite_primary_key and Dr. Nic.

James Byrne wrote:

Vincent.Z wrote:

i means primary_key(:tag_id,:book_id).like this.

On reflection, it seems that I may have misunderstood your real
question. A composite key is often called a natural key and it reflects
the contents of the relation in the table. So, for example, if you have
a translation model then your "natural key" might be language_code,
source_text) and the dependent attribute would be translated_text.

If what you are really asking for is how to set up a uniqueness
constraint on multiple attributes then you can enforce this at the DB
level by adding a unique composite index to the migration:

    add_index :books, :title, :edition, :publisher,
              :name => :idxU_books_edition,
              :unique => true

Which creates a unique index on the title, edition and publisher
attributes of the model books. To catch this inside rails before the DB
gives an error you will need to add a validates_uniqueness_of or custom
validation in the books model.

Note as well that index implementations are all DBMS dependent and that
creating an index does not necessarily provide any improvement in
retrievals.