# How to generate mapping with migration

**URL:** <https://www.ruby-forum.com/t/how-to-generate-mapping-with-migration/57154>\
**Category:** Rails\
**Created:** [April 7, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/how-to-generate-mapping-with-migration/57154 "2006-04-07T15:59:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_VSSSSlkerts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@Daniel\_VSSSSlkerts](https://www.ruby-forum.com/u/Daniel_VSSSSlkerts)\
**Post date:** [April 7, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/how-to-generate-mapping-with-migration/57154/1 "2006-04-07T15:59:45Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Daniel\_VSSSSlkerts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@Daniel\_VSSSSlkerts](https://www.ruby-forum.com/u/Daniel_VSSSSlkerts)\
**Post date:** [April 7, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/how-to-generate-mapping-with-migration/57154/2 "2006-04-07T18:05:32Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Daniel\_VSSSSlkerts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@Daniel\_VSSSSlkerts](https://www.ruby-forum.com/u/Daniel_VSSSSlkerts)\
**Post date:** [April 7, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/how-to-generate-mapping-with-migration/57154/3 "2006-04-07T18:14:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Daniel\_VSSSSlkerts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@Daniel\_VSSSSlkerts](https://www.ruby-forum.com/u/Daniel_VSSSSlkerts)\
**Post date:** [April 7, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/how-to-generate-mapping-with-migration/57154/4 "2006-04-07T18:39:16Z")

</div>

> 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/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![Daniel\_VSSSSlkerts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@Daniel\_VSSSSlkerts](https://www.ruby-forum.com/u/Daniel_VSSSSlkerts)\
**Post date:** [April 8, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/how-to-generate-mapping-with-migration/57154/5 "2006-04-08T12:49:56Z")

</div>

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,
