hi I have a table where the primarey key consists of two tables. lets say I have this colums customer VARCHAR countryIso VARHCAR name VARCHAR street VARCHAR and the key is a combination of customer and countryIso. I know this is really bad database design but this is a legacy database I have to use and there is no way changing the table and the key. can this be done with ActiveRecord. I haven''t found anything in the docs. maybe I have just overlooked it. regards Markus ___________________________________________________________ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de
On Jul 28, 2005, at 1:15 AM, Markus Jais wrote:> I have a table where the primarey key consists > of two tables. > lets say I have this colums > > customer VARCHAR > countryIso VARHCAR > name VARCHAR > street VARCHAR > > and the key is a combination of customer and > countryIso. > > I know this is really bad database design but this > is a legacy database I have to use and there is no > way changing the table and the key. > > can this be done with ActiveRecord. I haven''t found > anything in the docs. maybe I have just overlooked it.ActiveRecord has no support for compound primary keys. If you need to support a legacy database with this kind of schema, you''ll need to use a different ORM package (or roll your own). Sad, but true. :( - Jamis
Markus, I have seen work arounds where the find methods and other required methods are overridden for the specific model. Alessandro -----Mensagem original----- De: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] Em nome de Jamis Buck Enviada em: quinta-feira, 28 de julho de 2005 11:25 Para: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Assunto: Re: [Rails] compound primarey key and ActiveRecord ?? On Jul 28, 2005, at 1:15 AM, Markus Jais wrote:> I have a table where the primarey key consists > of two tables. > lets say I have this colums > > customer VARCHAR > countryIso VARHCAR > name VARCHAR > street VARCHAR > > and the key is a combination of customer and > countryIso. > > I know this is really bad database design but this > is a legacy database I have to use and there is no > way changing the table and the key. > > can this be done with ActiveRecord. I haven''t found > anything in the docs. maybe I have just overlooked it.ActiveRecord has no support for compound primary keys. If you need to support a legacy database with this kind of schema, you''ll need to use a different ORM package (or roll your own). Sad, but true. :( - Jamis _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails -- No virus found in this incoming message. Checked by AVG Anti-Virus. Version: 7.0.338 / Virus Database: 267.9.6/59 - Release Date: 27/7/2005
> ActiveRecord has no support for compound primary keys. If you need to > support a legacy database with this kind of schema, you''ll need to > use a different ORM package (or roll your own).Rather than have an action that does a Model.find(id), just have an action that does Model.find(:first, [:conditions => ''pkey1 = ? and pkey2 = ?'', pkey1, pkey2] Updates and deletes work in a similar way. The main challenge is on the insert; for that you either have to generate your sequential id, or otherwise provide all the keys.
On 7/29/05, Steve Downey <sldowney-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote:> > ActiveRecord has no support for compound primary keys. If you need to > > support a legacy database with this kind of schema, you''ll need to > > use a different ORM package (or roll your own). > > Rather than have an action that does a Model.find(id), just have an action that > does Model.find(:first, [:conditions => ''pkey1 = ? and pkey2 = ?'', pkey1, pkey2] > > Updates and deletes work in a similar way. The main challenge is on the insert; > for that you either have to generate your sequential id, or otherwise provide > all the keys.While this may work, you won''t be able to reference this class from any other associations. Compound Keys and Object Oriented identity don''t mix, even full ORM solutions like hibernate and toplink don''t really support them. -- Cheers Koz
> While this may work, you won''t be able to reference this class from > any other associations. > > Compound Keys and Object Oriented identity don''t mix, even full ORM > solutions like hibernate and toplink don''t really support them. >Yep. So when I want the associations I do: child_rows = ChildTable.find(:all, :conditions => [ based on parent keys ]) Not as nice as if Rails did all the fetching but not so bad when you consider all the other Rails goodies that will still work for you. If you can make data model changes, then do so. But if the options are don''t use Rails, or don''t use *some* of Rails, the choice is easy for me. In my case, all the legacy apps w/o auto sequencing keys I only need to do inquiry on.
Forgive me if this suggestion is obvious, or if it doesn''t fit your environment, but.. I''ve got a bajillion rows worth of legacy data to interface to, spread across hundreds of tables. To make it Rails-centric, I''ve just had people create Oracle views where necessary. This preserves the legacy structure, but lets me use all the fun AR helpers to navi-wiggle through it. Some of the syntax does scare, me, though.. like when I do: legacy_object.something = some_new_object. The syntax makes it ''look'' like I''m about to update the inquiry-only data, when in reality I''m just setting some legacy_whatever_id in the new Rails-centric data store. --Wilson. Steve Downey wrote:>>While this may work, you won''t be able to reference this class from >>any other associations. >> >>Compound Keys and Object Oriented identity don''t mix, even full ORM >>solutions like hibernate and toplink don''t really support them. >> > > > Yep. So when I want the associations I do: > > child_rows = ChildTable.find(:all, :conditions => [ based on parent keys ]) > > Not as nice as if Rails did all the fetching but not so bad when you consider > all the other Rails goodies that will still work for you. If you can make > data model changes, then do so. But if the options are don''t use Rails, > or don''t use *some* of Rails, the choice is easy for me. > > In my case, all the legacy apps w/o auto sequencing keys I only need to > do inquiry on. >
Deirdre Saoirse Moen
2005-Jul-28 20:32 UTC
Re: Re: compound primarey key and ActiveRecord ??
On Thu, 28 Jul 2005, Steve Downey wrote:> > Compound Keys and Object Oriented identity don''t mix, even full ORM > > solutions like hibernate and toplink don''t really support them.But adding an id field just to be Railsy breaks relational integrity, which is why I don''t add a field my data model doesn''t need (after all, if you really have a compound key, you really have a compound key).> Yep. So when I want the associations I do: > > child_rows = ChildTable.find(:all, :conditions => [ based on parent keys ]) > > Not as nice as if Rails did all the fetching but not so bad when you > consider all the other Rails goodies that will still work for you. If > you can make data model changes, then do so....IF they happen to make sense for your application.> But if the options are don''t use Rails, or don''t use *some* of Rails, > the choice is easy for me.Ditto.> In my case, all the legacy apps w/o auto sequencing keys I only need to > do inquiry on.In my case, some new apps with some moderately complex (for me, 10-100 tables is "moderate"; the set of four apps I''m working on has around 35 tables) data models. Some of those tables have compound keys of necessity. I doubt there''s any way around this. -- _Deirdre web / blog: http://deirdre.net/ yarn: http://fuzzyorange.com cat''s blog: http://fuzzyorange.com/vsd/ "Memes are a hoax! Pass it on!"
hi thanks all who answered. as some have already written, find works just fine for my application. and fortunately this is all I need for now. when I need update and delete later I will use your advice and tips. thanks Markus --- Markus Jais <markus_jais-LWAfsSFWpa4@public.gmane.org> schrieb:> hi > > I have a table where the primarey key consists > of two tables. > lets say I have this colums > > customer VARCHAR > countryIso VARHCAR > name VARCHAR > street VARCHAR > > and the key is a combination of customer and > countryIso. > > I know this is really bad database design but this > is a legacy database I have to use and there is no > way changing the table and the key. > > can this be done with ActiveRecord. I haven''t found > anything in the docs. maybe I have just overlooked > it. > > regards > > Markus > > > > > > > >___________________________________________________________> > Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher > kostenlos - Hier anmelden: http://mail.yahoo.de > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >___________________________________________________________ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de