Forum: Ruby on Rails ActiveRecord setting IDs on auto increment fields

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Mr.Jay Ellis Wiggins (Guest)
on 2006-01-05 20:57
(Received via mailing list)
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:

def update
     if @params[:risk_id]
       @industry.risks = Risk.find(@params[:risk_id]) if @params

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!


#### 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 :^)
Jay W. (Guest)
on 2006-01-06 00:21
(Received via mailing list)
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...
Jay Wiggins (Guest)
on 2006-01-06 06:25
(Received via mailing list)
shoot, i think i've found my problem:

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

This topic is locked and can not be replied to.