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,
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,
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?
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/
Rimantas L. 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,