All, I could not find an answer to the following questions on using Rails: 1) It seems as though the ID column is required for all Database tables. Is there a way around this when you have a pre-existing database? 2) How does RAILS handle tables with compound primary keys, i.e. multiple columns make up the primary key? 3) I have the following tables: Table A id fk1_ref_b_id fk2_ref_b_id Table B id desc where the columns fk1_ref_b_id and fk2_ref_b_id both reference Table B's ID column? If there is not a current solution to these problems is there any in the works? Or what would it take to implement these in a way that is transparent the developer? Thanks in advance, Ron _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I don''t think AR supports tables with compound primary keys or no primary keys. You might look at Og, another ruby data mapper. http://www.rubygarden.org/index.cgi/Libraries/og_tutorial.rdoc On 8/11/05, Ron DiFrango <rdifrango-wfttzDtloRfhmZgEOWDAxUEOCMrvLtNR@public.gmane.org> wrote:> All, > > I could not find an answer to the following questions on using Rails: > > 1) It seems as though the ID column is required for all Database tables. Is there a way around this when you have a pre-existing database? > > 2) How does RAILS handle tables with compound primary keys, i.e. multiple columns make up the primary key? > > 3) I have the following tables: > > Table A > id > fk1_ref_b_id > fk2_ref_b_id > > Table B > id > desc > > where the columns fk1_ref_b_id and fk2_ref_b_id both reference Table B''s ID column? > > If there is not a current solution to these problems is there any in the works? Or what would it take to implement these in a way that is transparent the developer? > > Thanks in advance, > > Ron > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- rick http://techno-weenie.net
Thanks Rick, but the question becomes does it work seemlessly within RAILS? If so how? -----Original Message----- From: Rick Olson [mailto:technoweenie@gmail.com] Sent: Thu 8/11/2005 5:33 PM To: rails@lists.rubyonrails.org Cc: Subject: Re: [Rails] ID Column I don't think AR supports tables with compound primary keys or no primary keys. You might look at Og, another ruby data mapper. http://www.rubygarden.org/index.cgi/Libraries/og_tutorial.rdoc On 8/11/05, Ron DiFrango <rdifrango@captechventures.com> wrote: > All, > > I could not find an answer to the following questions on using Rails: > > 1) It seems as though the ID column is required for all Database tables. Is there a way around this when you have a pre-existing database? > > 2) How does RAILS handle tables with compound primary keys, i.e. multiple columns make up the primary key? > > 3) I have the following tables: > > Table A > id > fk1_ref_b_id > fk2_ref_b_id > > Table B > id > desc > > where the columns fk1_ref_b_id and fk2_ref_b_id both reference Table B's ID column? > > If there is not a current solution to these problems is there any in the works? Or what would it take to implement these in a way that is transparent the developer? > > Thanks in advance, > > Ron > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > -- rick http://techno-weenie.net _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 8/11/05, Ron DiFrango <rdifrango-wfttzDtloRfhmZgEOWDAxUEOCMrvLtNR@public.gmane.org> wrote:> Thanks Rick, but the question becomes does it work seemlessly within RAILS? If so how?Just require the lib in environment.rb and define the models in app/models. You won''t be able to use some of the AR-specific stuff obviously (validations, error messages), unless og happens to support the same API. There''s nothing in rails that forces you to use ActiveRecord. -- rick http://techno-weenie.net
On 8/12/05, Ron DiFrango <rdifrango-wfttzDtloRfhmZgEOWDAxUEOCMrvLtNR@public.gmane.org> wrote:> All, > > I could not find an answer to the following questions on using Rails: > > 1) It seems as though the ID column is required for all Database tables. Is there a way around this when you have a pre-existing database?Not a pretty one> 2) How does RAILS handle tables with compound primary keys, i.e. multiple columns make up the primary key?No, most ORM tools don''t work with compound keys, high end solutions like hibernate and toplink (java) support it, but all the documentation says "only use these if you really really have to".> 3) I have the following tables: > > Table A > id > fk1_ref_b_id > fk2_ref_b_id > > Table B > id > desc > > where the columns fk1_ref_b_id and fk2_ref_b_id both reference Table B''s ID column?This one is easy: class A < ActiveRecord::Base belongs_to :b_one, :class_name=>"B", :foreign_key => "fk1_ref_b_id" belongs_to :b_two, :class_name=>"B", :foreign_key => "fk2_ref_b_id" end> If there is not a current solution to these problems is there any in the works? Or what would it take to implement these in a way that is transparent the developer?I''ve posted several times about the benefits of using surrogate keys (id), If you *really really really* can''t change the schema, I''d recommend using something other than Active Record to do your persistence having said that, some people have had luck mapping to legacy schemas. -- Cheers Koz
Koz, Thanks for your response below. That helps alot! Just to comment and discuss further see my answers below. Thanks, Ron -----Original Message----- From: Michael Koziarski [mailto:koziarski@gmail.com] Sent: Thu 8/11/2005 7:02 PM To: rails@lists.rubyonrails.org Cc: Subject: Re: [Rails] ID Column On 8/12/05, Ron DiFrango <rdifrango@captechventures.com> wrote: > All, > > I could not find an answer to the following questions on using Rails: > > 1) It seems as though the ID column is required for all Database tables. Is there a way around this when you have a pre-existing database? Koz: Not a pretty one RRD: To some degree, I still have a problem with the ID deal. I relooked at most of my tables and a large number of them had an ID field, it is just that it is name tablename_id instead of just ID. I guess I could migrate it, but that would require change 50+ tables. Plus some of them I have varchar as the primary key. > 2) How does RAILS handle tables with compound primary keys, i.e. multiple columns make up the primary key? Koz: No, most ORM tools don't work with compound keys, high end solutions Koz: like hibernate and toplink (java) support it, but all the Koz: documentation says "only use these if you really really have to". RRD: Too bad Hibernate is what I am used as I am a Java/J2EE developer looking to convert to Rub on RAILS :-) > 3) I have the following tables: > > Table A > id > fk1_ref_b_id > fk2_ref_b_id > > Table B > id > desc > > where the columns fk1_ref_b_id and fk2_ref_b_id both reference Table B's ID column? Koz: This one is easy: Koz: class A < ActiveRecord::Base Koz: belongs_to :b_one, :class_name=>"B", :foreign_key => "fk1_ref_b_id" Koz: belongs_to :b_two, :class_name=>"B", :foreign_key => "fk2_ref_b_id" Koz: end RRD: Works for me! > If there is not a current solution to these problems is there any in the works? Or what would it take to implement these in a way that is transparent the developer? Koz: I've posted several times about the benefits of using surrogate keys Koz: (id), If you *really really really* can't change the schema, I'd Koz: recommend using something other than Active Record to do your Koz: persistence having said that, some people have had luck mapping to Koz: legacy schemas. RRD: Can send me your other posts on the subject? -- Cheers Koz _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Thu, 11 Aug 2005, Rick Olson wrote:> I don''t think AR supports tables with compound primary keys or no > primary keys. You might look at Og, another ruby data mapper. > http://www.rubygarden.org/index.cgi/Libraries/og_tutorial.rdocIt does support compound primary keys, but not fully automagically. That said, it''s not horribly painful. (See my prior post about same) -- _Deirdre web / blog: http://deirdre.net/ yarn: http://fuzzyorange.com cat''s blog: http://fuzzyorange.com/vsd/ "Memes are a hoax! Pass it on!"
Has anyone here actually worked with Og, or used it with Rails? How does it compare to AR? I''ve read the docs, but not had a chance to try it yet. Cheers, Colin On 12/08/05, Deirdre Saoirse Moen <deirdre-mzk6fgDMp2XR7s880joybQ@public.gmane.org> wrote:> On Thu, 11 Aug 2005, Rick Olson wrote: > > > I don''t think AR supports tables with compound primary keys or no > > primary keys. You might look at Og, another ruby data mapper. > > http://www.rubygarden.org/index.cgi/Libraries/og_tutorial.rdoc > > It does support compound primary keys, but not fully automagically. That > said, it''s not horribly painful. (See my prior post about same) > > -- > _Deirdre web / blog: http://deirdre.net/ > yarn: http://fuzzyorange.com cat''s blog: http://fuzzyorange.com/vsd/ > "Memes are a hoax! Pass it on!" > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi Ron,> RRD: To some degree, I still have a problem with the ID deal. I relooked at most of my tables and a large number of them had an ID field, it is just that it is name tablename_id instead of just ID. I guess I could migrate it, but that would require change 50+ tables. Plus some of them I have varchar as the primary key.If you have a single ID field, you can configure Active Record to use it using Model.id_column(). It''s not well documented, but the Pragmatic Rails book talks about it. There are a few quirks, I can''t remember off the top of my head (don''t have the book right here) but I believe you have to refer the the field as "id" within Rails even though it''s called something else in the DB. I also think that you have to set the field explicitly yourself before saving the object.> RRD: Can send me your other posts on the subject?There''s a searchable archive of the list at: http://news.gmane.org/gmane.comp.lang.ruby.rails. Unfortunately the search seems to be broken at the moment... Cheers, Colin