Help me create primary_key with ActiveRecord::Migration

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

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

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.

-----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. :wink:


Phillip G.
Twitter: 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs