Rich Barton
2005-Feb-09 13:48 UTC
Strange has_and_belongs_to_many behaviour (or is it just me?)
Hi all,
My Rails app is giving an error whenever it tries to add AR objects to
a collection (has_and_belongs_to_many association between Posts and
Labels). The application code is something along the lines of
@post.labels<<collection_of_labels. The error occurs when Rails tries
to persist the association by INSERTing a record into the join table.
What it appears to be trying to do is to insert a value for the primary
key (`id`) for this table, though this doesn''t make sense as `id` is an
auto_increment column. In all test cases, the value it is trying to add
to the id column happens to be the same as the foreign key value for
the record that is being added _to_ the collection, eg:
ActiveRecord::StatementInvalid (Duplicate entry ''5'' for key 1:
INSERT
INTO labels_posts (`post_id`, `id`, `label_id`) VALUES (''8'',
5, ''5'')):
I''ve narrowed down the source of the problem to 2 lines in the
following file (I''m using the Rails gem). I''ve snipped the
offending
section from the source to give you guys some context. Commenting out
lines 121-122 resolves the issue.
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/
associations/has_and_belongs_to_many_association.rb
118: when @association_foreign_key
119: attributes[column.name] = record.quoted_id
120: else
121: value = record[column.name]
122: attributes[column.name] = value unless value.nil?
123: end
124: attributes
125: end
So, is this a bug? What do those two lines do? Could something else be
causing the error (like my sloppy coding :)? Your thoughts and comments
are most welcome.
If you need any more information on my setup or code or anything, let
me know.
Rich Barton
Jens-Christian Fischer
2005-Feb-09 14:00 UTC
Re: Strange has_and_belongs_to_many behaviour (or is it just me?)
> What it appears to be trying to do is to insert a value for the primary > key (`id`) for this table, though this doesn''t make sense as `id` is an[...]> ActiveRecord::StatementInvalid (Duplicate entry ''5'' for key 1: INSERT > INTO labels_posts (`post_id`, `id`, `label_id`) VALUES (''8'', 5, ''5'')):The association table doesn''t need an id field. The post_id and label_id are quite enough. Make a "compound key" of the two columns and you should be all set Jens-Christian
Rich Barton
2005-Feb-09 14:08 UTC
Re: Strange has_and_belongs_to_many behaviour (or is it just me?)
On 9 Feb 2005, at 15:00, Jens-Christian Fischer wrote:>> What it appears to be trying to do is to insert a value for the >> primary >> key (`id`) for this table, though this doesn''t make sense as `id` is >> an > [...] >> ActiveRecord::StatementInvalid (Duplicate entry ''5'' for key 1: INSERT >> INTO labels_posts (`post_id`, `id`, `label_id`) VALUES (''8'', 5, ''5'')): > > The association table doesn''t need an id field. The post_id and > label_id are quite enough. Make a "compound key" of the two columns > and you should be all set > > Jens-ChristianSo it was just me then. Thanks Jens-Christian! Rich Barton
Corey Lawson
2005-Feb-13 09:55 UTC
Re: Strange has_and_belongs_to_many behaviour (or is it just me?)
But what if you want to have a separate primary key, besides the unique set of the composite foreign keys, in the association table, because you think you have a good reason or few to want to hide/not use the composite key for other relationships? On Wed, 9 Feb 2005 15:08:34 +0100, Rich Barton <richbarton-ee4meeAH724@public.gmane.org> wrote:> On 9 Feb 2005, at 15:00, Jens-Christian Fischer wrote: > > >> What it appears to be trying to do is to insert a value for the > >> primary > >> key (`id`) for this table, though this doesn''t make sense as `id` is > >> an > > [...] > >> ActiveRecord::StatementInvalid (Duplicate entry ''5'' for key 1: INSERT > >> INTO labels_posts (`post_id`, `id`, `label_id`) VALUES (''8'', 5, ''5'')): > > > > The association table doesn''t need an id field. The post_id and > > label_id are quite enough. Make a "compound key" of the two columns > > and you should be all set > > > > Jens-Christian > > So it was just me then. > > Thanks Jens-Christian! > > Rich Barton > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Tim Bates
2005-Feb-13 10:05 UTC
Re: Strange has_and_belongs_to_many behaviour (or is it just me?)
Corey Lawson wrote:> But what if you want to have a separate primary key, besides the > unique set of the composite foreign keys, in the association table, > because you think you have a good reason or few to want to hide/not > use the composite key for other relationships?If you''re trying to make the habtm do things it wasn''t designed to do, like add lots of attributes or mess around with complex behaviour, then 90% of the time there''s a good case for creating another model for the join table and having two has_many/belongs_to relationships instead of one habtm. Tim. -- Tim Bates tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org