Mr.Jay Ellis Wiggins
2006-Jan-05 17:15 UTC
[Rails] ActiveRecord setting IDs on auto increment fields
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 :^)
Jay Wiggins
2006-Jan-05 22:18 UTC
[Rails] ActiveRecord setting IDs on auto increment fields
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 >
Jay Wiggins
2006-Jan-06 04:24 UTC
[Rails] ActiveRecord setting IDs on auto increment fields
shoot, i think i''ve found my problem: 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. On Jan 5, 2006, at 3:18 PM, Jay Wiggins wrote:> > 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 >> > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Alan C Francis
2006-Jan-11 20:47 UTC
[Rails] ActiveRecord setting IDs on auto increment fields
Begin forwarded message:> Subject: Re: [Rails] ActiveRecord setting IDs on auto increment fields > To: rails@lists.rubyonrails.org > Message-ID: <B7F7BC8E-240F-44C3-933A-0C4BAD273D8D@corpedia.com> > Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed > > > 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...I''m fumbling, but ISTR that link tables weren''t supposed have an id column...something to do with it interfering with the id column on the primary party. Or rather, if you needed extra attributes on the join, it needed it''s own model and the default magic associations stopped working. A. -- mailto:alancfrancis@gmail.com http://blog.alancfrancis.com/