Forum: Ruby on Rails How to generate mapping with migration

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.
89dc62dd0a40f43a75d73e8059d61a91?d=identicon&s=25 Daniel Völkerts (Guest)
on 2006-04-07 15:59
(Received via mailing list)
Hi there, I've tried to add a m:n mapping table using the migration
mechanism.

ruby script\generate migration add_categories_notes_mapping

and filled the migration file with:

class AddNotesCategoriesMapping < ActiveRecord::Migration
   def self.up
		create_table :categories_notes do |t|
			t.column :category_id, :integer, :null=>false
			t.column :note_id, :integer, :null=>false
		end
   end

   def self.down
		drop_table :categories_notes
   end
end

Now, I'd like to define both fields as primary key, but after I read the
API documentation, I'll wonder how I could pass to parameters to the
:primary parameter of create_table.


Any hints appreciated,
C70264c74698cbe2324b91ff778abcff?d=identicon&s=25 Daniel Völkerts (Guest)
on 2006-04-07 18:05
(Received via mailing list)
Daniel Völkerts schrieb:

> Any hints appreciated,

Ohh, read the api carefully.

class AddNotesCategoriesMapping < ActiveRecord::Migration
   def self.up
		create_table :categories_notes, :id=>false do |t|
			t.column :category_id, :primary_key, :null=>false
			t.column :note_id, :primary_key, :null=>false
		end
   end

   def self.down
		drop_table :categories_notes
   end
end

Greetings,
C70264c74698cbe2324b91ff778abcff?d=identicon&s=25 Daniel Völkerts (Guest)
on 2006-04-07 18:14
(Received via mailing list)
Daniel Völkerts schrieb:
>             t.column :note_id, :primary_key, :null=>false
>         end
>   end
>
>   def self.down
>         drop_table :categories_notes
>   end
> end
>

No, that was not the solution... Any ideas?
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-04-07 18:39
(Received via mailing list)
> No, that was not the solution... Any ideas?

class AddNotesCategoriesMapping < ActiveRecord::Migration
  def self.up
      create_table (:categories_notes, :id=>false) do |t|
        t.column :category_id, :integer
        t.column :note_id, :integer
      end
      execute "ALTER TABLE `categories_notes` ADD PRIMARY KEY (
`category_id` , `note_id` )"
  end

  def self.down
        drop_table :categories_notes
  end
end

But I think simple
add_index(:categories_notes, [:category_id, :note_id], :unique =>true )

instead of "execute" should be ok in your situation if you want unique
combinations.


Regards,
Rimantas
--
http://rimantas.com/
C70264c74698cbe2324b91ff778abcff?d=identicon&s=25 Daniel Völkerts (Guest)
on 2006-04-08 12:49
(Received via mailing list)
Rimantas Liubertas schrieb:

> But I think simple
> add_index(:categories_notes, [:category_id, :note_id], :unique =>true )
>
> instead of "execute" should be ok in your situation if you want unique
> combinations.

Thank you, you helped me a lot. *g*


Nice weekend,
This topic is locked and can not be replied to.