[Rails] ActiveRecord setting IDs on auto increment fields

Jay Wiggins
2006-01-05T22:18:56Z

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...


On Jan 5, 2006, at 10:15 AM, Mr.Jay Ellis Wiggins wrote:

>
> 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 :^)
>
>
>
>
>
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>