# HABTM setup problem?

**URL:** <https://www.ruby-forum.com/t/habtm-setup-problem/135411>\
**Category:** Rails\
**Created:** [April 21, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/habtm-setup-problem/135411 "2008-04-21T23:27:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [April 21, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/habtm-setup-problem/135411/1 "2008-04-21T23:27:36Z")

</div>

Hey,  
So I have a HABTM relationship between story and genre with a join  
table genres\_stories. From what I can tell the first story I add  
genres to works fine, but when I try to add genres to a second stroy I  
get the following error:

PRIMARY KEY must be unique: INSERT INTO genres\_stories (“genre\_id”,  
“story\_id”, “id”) VALUES (1, 2, 1)

any ideas?

Thanks,  
JB

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [April 21, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/habtm-setup-problem/135411/2 "2008-04-21T23:32:20Z")

</div>

On 21 Apr 2008, at 22:26, Jables wrote:

> Hey,  
> So I have a HABTM relationship between story and genre with a join  
> table genres\_stories. From what I can tell the first story I add  
> genres to works fine, but when I try to add genres to a second stroy I  
> get the following error:
> 
> PRIMARY KEY must be unique: INSERT INTO genres\_stories (“genre\_id”,  
> “story\_id”, “id”) VALUES (1, 2, 1)

If you’ve got hatbm (as opposed to has many through) you don’t want an  
id column.

Fred

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [April 21, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/habtm-setup-problem/135411/3 "2008-04-21T23:34:41Z")

</div>

On 4/21/08, Jables [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So I have a HABTM relationship between story and genre with a join  
> table genres\_stories. From what I can tell the first story I add  
> genres to works fine, but when I try to add genres to a second stroy I  
> get the following error:
> 
> PRIMARY KEY must be unique: INSERT INTO genres\_stories (“genre\_id”,  
> “story\_id”, “id”) VALUES (1, 2, 1)
> 
> any ideas?

A join table doesn’t typically require a primary key id field. If you  
disagree then you at least need to base it on a sequence or some other  
auto-incrementing feature of your database.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [April 23, 2008, 3:25am UTC](https://www.ruby-forum.com/t/habtm-setup-problem/135411/4 "2008-04-23T03:25:52Z")

</div>

So here is my migration:

```
    create_table :genres_stories, :force => true do |t|
      t.references :story, :genre
    end

```

it is automatically putting that id field into the table.

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [April 23, 2008, 3:27am UTC](https://www.ruby-forum.com/t/habtm-setup-problem/135411/5 "2008-04-23T03:27:03Z")

</div>

nevermind, figured it out:

FYI the migration is  
create\_table :genres\_stories, :id =\> false do |t|  
t.references :story, :genre  
end
