# Using Migrations to convert join table to join model

**URL:** <https://www.ruby-forum.com/t/using-migrations-to-convert-join-table-to-join-model/58350>\
**Category:** Rails\
**Created:** [April 20, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/using-migrations-to-convert-join-table-to-join-model/58350 "2006-04-20T20:02:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 20, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/using-migrations-to-convert-join-table-to-join-model/58350/1 "2006-04-20T20:02:15Z")

</div>

Greetings,

I’m trying to convert a HABTM w/attributes join table in my database  
into a join model table in order to utilize the new has\_many :through  
functionality introduced in Rails 1.1. Here’s the current table  
definition:

licenses\_user\_groups: license\_id, user\_group\_id, usage\_notes

Perhaps it was a mistake, but I made (license\_id, user\_group\_id) the  
primary key.

Now, after playing with has\_many :through, I have discovered that, among  
other modifications, I really need to add an id column to the table and  
make it the primary key. However, I cannot figure out how to use the  
SchemaStatements to add the new column, set it to auto-increment, and  
then redefine the primary key to be the id column instead of the  
(license\_id, user\_group\_id) columns.

I could use the execute statement and use SQL directly, but I would  
prefer not to (one of the reasons behind migrations, I thought, was to  
provide a DB independent way of creating and modifying schemas; using  
SQL directly makes it possible and likely that database specific changes  
will be introducted).

Any help (or confirmation that execute is the only way to do what I’m  
looking to do) would be appreciated.

Thanks.

David.

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 20, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/using-migrations-to-convert-join-table-to-join-model/58350/2 "2006-04-20T22:05:39Z")

</div>

Anyone out there?

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 20, 2006, 10:23pm UTC](https://www.ruby-forum.com/t/using-migrations-to-convert-join-table-to-join-model/58350/3 "2006-04-20T22:23:27Z")

</div>

Hello David,

> provide a DB independent way of creating and modifying schemas;  
> using  
> SQL directly makes it possible and likely that database specific changes  
> will be introducted).

Maybe I’m wrong but i don’t think it’s possible without a direct  
SQL execute statement. For me, the primary key is created  
during a #create\_table call. And there are no methods to suppress  
or create a primary key (or I haven’t found them yet).

The only way I see : create another table with a primary key id,  
transfer the date into it, suppress the old join model table.

Not nice.

The simplest way is the SQL statement…

```
-- Jean-FranÃ§ois.
```

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 20, 2006, 10:40pm UTC](https://www.ruby-forum.com/t/using-migrations-to-convert-join-table-to-join-model/58350/4 "2006-04-20T22:40:17Z")

</div>

Jean-FranÃ§ois wrote:

> Maybe I’m wrong but i don’t think it’s possible without a direct  
> SQL execute statement. For me, the primary key is created  
> during a #create\_table call. And there are no methods to suppress  
> or create a primary key (or I haven’t found them yet).
> 
> The only way I see : create another table with a primary key id,  
> transfer the date into it, suppress the old join model table.
> 
> Not nice.

Yep, that sounds about right to me.

There’s a bug in trac to support this funcitonality. Didn’t make it into  
1.1, and it’s now slated for 1.2.  
[http://dev.rubyonrails.org/ticket/3735](http://dev.rubyonrails.org/ticket/3735)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 21, 2006, 12:25am UTC](https://www.ruby-forum.com/t/using-migrations-to-convert-join-table-to-join-model/58350/5 "2006-04-21T00:25:57Z")

</div>

Josh S. wrote:  
Jean-FranÃ§ois wrote:

Thanks for your responses; it is what I feared, but at least I can do  
what I need to.

Am I just weird? I can’t be the only person running into all of these  
has\_many :through issues and using migrations to convert a HABTM join  
table into a has\_many :through join model. I’ve also had issues with  
migrations not support foreign keys and such; my experiences are making  
me feel like a lot of RoR features are rather incomplete.

Like I said, I appreciate the confirmation both of you provided.

David.

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 21, 2006, 1:22am UTC](https://www.ruby-forum.com/t/using-migrations-to-convert-join-table-to-join-model/58350/6 "2006-04-21T01:22:46Z")

</div>

David W. wrote:

> Am I just weird? I can’t be the only person running into all of these  
> has\_many :through issues and using migrations to convert a HABTM join  
> table into a has\_many :through join model. I’ve also had issues with  
> migrations not support foreign keys and such; my experiences are making  
> me feel like a lot of RoR features are rather incomplete.

:through associations are still a relatively new feature, and there are  
some facets that are incomplete. When you run into missing piece, the  
best thing you can do is to make sure there is a ticket in trac. Either  
create a new ticket, or add any pertinent information that is missing  
from an existing ticket. Test cases can help. And if want to submit a  
patch (with test cases) to fix the problem, go for it!

I gotta say though, you may be borrowing trouble here. Rather than do  
things the “right” way using database agnostic migrations, just do the  
simplest thing that will work (without causing you grief later on).

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
