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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Roger Pack
2008-Apr-16 04:52 UTC
Re: Help me create primary_key with ActiveRecord::Migration
Tuan Minh 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 > .............. > endI believe by default rails will create a primary key for you -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bryan Ray
2008-Apr-16 04:56 UTC
Re: Help me create primary_key with ActiveRecord::Migration
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. On Apr 15, 2008, at 5:02 AM, Tuan Minh 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 > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Phillip Gawlowski
2008-Apr-16 05:07 UTC
Re: Help me create primary_key with ActiveRecord::Migration
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Bryan Ray 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. ;) - -- Phillip Gawlowski 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----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---