I was under the impression that Rails liked every table to have an "id" field. But now I read in the has_or_belongs_to_many documentation in the API docs that this can, in fact, cause undesirable effects for join tables due to clobbering the "id" fields in both tables that have the has_or_belongs_to_many relation. I had come under the impression that it is good practice to give each of my tables in PgSQL at least an id field as a primary key. Are join tables an exception to this rule? Before going ahead and deleting all IDs on my join tables, I'd like to hear someone else's opinion on this :-) It's actually on this very ML that I was confinced of the usefullness of never using anything but a simple SERIAL as a primary key, and hey, I might just have been confinced a bit too thoroughly ;-) - Rowan -- Morality is usually taught by the immoral. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> I had come under the impression that it is good practice to give each > of my tables in PgSQL at least an id field as a primary key. Are join > tables an exception to this rule?One of the primary uses of a primary key is to help the database (and end user) quickly find a unique row in a table. Join tables (for one-to-many and many-to-many relationships) are usually used to return a set of rows that define relationships between tables. Because of this, a unique primary key isn''t as important. In fact, I don''t think I have any primary keys in any of my join tables. -- Mando _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 10/8/05, Mando Escamilla <mando.escamilla@gmail.com> wrote:> > I had come under the impression that it is good practice to give each > > of my tables in PgSQL at least an id field as a primary key. Are join > > tables an exception to this rule? > > One of the primary uses of a primary key is to help the database (and end > user) quickly find a unique row in a table. Join tables (for one-to-many > and many-to-many relationships) are usually used to return a set of rows > that define relationships between tables. Because of this, a unique primary > key isn't as important. In fact, I don't think I have any primary keys in > any of my join tables.Your reply relaxes my newly acquired development cramps. Now I don't have to suffer any more analysis paralysis. Greatly appreciated. Thank you, - Rowan -- Morality is usually taught by the immoral. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ive never used a join table for one to many. We are talking about intersect tables right? rails is smart enough that using the intersect table in a many to many with i think modelname_id as your two fieldnames will allow it to automagically do its thing. Sam On 10/7/05, Mando Escamilla <mando.escamilla-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > I had come under the impression that it is good practice to give each > > of my tables in PgSQL at least an id field as a primary key. Are join > > tables an exception to this rule? > > One of the primary uses of a primary key is to help the database (and end > user) quickly find a unique row in a table. Join tables (for one-to-many > and many-to-many relationships) are usually used to return a set of rows > that define relationships between tables. Because of this, a unique primary > key isn''t as important. In fact, I don''t think I have any primary keys in > any of my join tables. > > -- > Mando > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 8-okt-2005, at 1:54, BigSmoke wrote:> I was under the impression that Rails liked every table to have an > "id" field. But now I read in the has_or_belongs_to_many documentation > in the API docs that this can, in fact, cause undesirable effects for > join tables due to clobbering the "id" fields in both tables that have > the has_or_belongs_to_many relation.It does clobber, in fact I have been affected by this and some unit testing sessions have been especially troublesome :-) To be more precise, it clobbers the "id" on fetched models in collections (so that when you fetch Thing''s Tags for instance their IDs get mangled). This happens primarily because Rails is (IMO) a bit too-aggressive on * selects, while instead it should take the time to compile a list of distinctive columns itself. It already does when eager-loading as I know. If you want fast searches on join tables I think you can make a unique index on the fields it joins (assuming you only can have one row joinint two records from other tables together). As soon as your join table evolves into a full-fledged model you can safely add an ID to it. -- Julian "Julik" Tarkhanov