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
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 :^)