# ActiveRecord setting IDs on auto increment fields

**URL:** <https://www.ruby-forum.com/t/activerecord-setting-ids-on-auto-increment-fields/56281>\
**Category:** Rails\
**Created:** [January 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/activerecord-setting-ids-on-auto-increment-fields/56281 "2006-01-05T19:57:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr.Jay\_Ellis\_Wiggin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@Mr.Jay\_Ellis\_Wiggin](https://www.ruby-forum.com/u/Mr.Jay_Ellis_Wiggin)\
**Post date:** [January 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/activerecord-setting-ids-on-auto-increment-fields/56281/1 "2006-01-05T19:57:30Z")

</div>

Hi All, I’ve tried to do my searches, but can’t find reference to  
this anywhere - My question is about ActiveRecord inserts and IDs. I  
have a join table that both the models of the source tables join with  
“has\_and\_belongs\_to\_many”. It seems like ActiveRecord is setting the  
“id” field for the join table- This should be an auto\_increment and  
ActiveRecord shouldn’t need to force a value, no?

the table looks like this:

CREATE TABLE `industries_risks` (  
`id` int(10) unsigned NOT NULL auto\_increment,  
`industry_id` int(10) unsigned default NULL,  
`risk_id` int(10) unsigned default NULL,  
`created_on` datetime default NULL,  
`updated_on` datetime default NULL,  
PRIMARY KEY (`id`)  
) TYPE=InnoDB;

the code in question is almost directly ripped from this tutorial:  
[http://jrhicks.net/Projects/rails/has\_many\_and\_belongs\_to\_many.pdf](http://jrhicks.net/Projects/rails/has_many_and_belongs_to_many.pdf)

def update  
…  
if @params[:risk\_id]  
@industry.risks = Risk.find(@params[:risk\_id]) if @params  
[:risk\_id]  
else  
@industry.risks.clear  
end  
…  
end

and the error looks like this- NOTICE the `id` field getting set to 3…

Mysql::Error: Duplicate entry ‘3’ for key 1:  
INSERT INTO industries\_risks  
(`created_on`, `updated_on`, `risk_id`, `id`, `industry_id`)  
VALUES (‘2006-01-04 10:33:19’, ‘2006-01-04 10:33:19’, 1, 3, 3)

Thanks for any light anyone can shed on this!

Jay.

#### NEWBY DISCLAIMER: I’ve been dev’ing web apps for a really long

time, but just now getting 'round to testing out Rails (and Ruby) -  
thanks for your patience :^)

---

<div class="post-metadata">

**Author:** ![Mr.Jay\_Ellis\_Wiggin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@Mr.Jay\_Ellis\_Wiggin](https://www.ruby-forum.com/u/Mr.Jay_Ellis_Wiggin)\
**Post date:** [January 5, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/activerecord-setting-ids-on-auto-increment-fields/56281/2 "2006-01-05T23:21:52Z")

</div>

ActiveRecord is creating it’s own IDs on an auto increment field in  
my link table- is this normal? I can’t wrap my mind around why / what  
to do about it…

---

<div class="post-metadata">

**Author:** ![Mr.Jay\_Ellis\_Wiggin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@Mr.Jay\_Ellis\_Wiggin](https://www.ruby-forum.com/u/Mr.Jay_Ellis_Wiggin)\
**Post date:** [January 6, 2006, 5:25am UTC](https://www.ruby-forum.com/t/activerecord-setting-ids-on-auto-increment-fields/56281/3 "2006-01-06T05:25:17Z")

</div>

shoot, i think i’ve found my problem:

[http://dev.rubyonrails.org/ticket/1031](http://dev.rubyonrails.org/ticket/1031)

i’m too newby to help here… :^(  
anyone have any other thoughts? i’ll try in the meantime without an  
id field in the join table

jay.
