Re: linking on differently-named foreign keys

[prev] [thread] [next] [Date index for 2005/02/14]

From: Ofer Nave
Subject: Re: linking on differently-named foreign keys
Date: 08:58 on 14 Feb 2005
Tony Bowden wrote:

>On Mon, Feb 14, 2005 at 12:38:47AM -0800, Ofer Nave wrote:
>  
>
>>CREATE TABLE account ( account_id INT );
>>CREATE TABLE message ( sender_id INT, receiver_id INT );
>>Let's say I want to create my Account class, and use Account->has_many() 
>>to defined the methods inbox() and sent_mail().  Is there an elegant way 
>>to tell CDBI to lookup the rows in the message table where 
>>receiver_id=account_id for inbox() and sender_id=account_id for sent_mail()?
>>    
>>
>
>You give has_many a 3rd argument for the key to use:
>
>Account->has_many(inbox => "Message", 'receiver_id');
>Account->has_many(sent_mail => "Message", 'account_id');
>
>Tony
>  
>
I was about to say "but that's not documented!", but then I took a 
closer look, and found it at the end of the last paragraph in that 
section.  :)  My bad.

Now, what about the Message class?  When I set up the two has_a 
relationships, does has_a also take a third argument so I can tell it to 
join to account_id and not sender_id/receiver_id, like this:

Message->has_a( sender => "Account", account_id );
Message->has_a( receiver => "Account", account_id );

Or does it figure it out from the third argument in has_many in the 
Account class?  (I just reread the docs on has_a twice to make sure I 
didn't miss an answer.)

-ofer

(message missing)

linking on differently-named foreign keys
Ofer Nave 08:38 on 14 Feb 2005

Re: linking on differently-named foreign keys
Tony Bowden 08:47 on 14 Feb 2005

Re: linking on differently-named foreign keys
Ofer Nave 08:58 on 14 Feb 2005

Re: linking on differently-named foreign keys
Tony Bowden 09:50 on 14 Feb 2005

Re: linking on differently-named foreign keys
Edward J. Sabol 20:00 on 14 Feb 2005

Generated at 17:31 on 15 Feb 2005 by mariachi v0.52