Re: Foreign key as primary key...
[prev]
[thread]
[next]
[Date index for 2005/02/22]
--- Ofer Nave <onave@xxxxxxxxx.xxx> wrote:
> Perhaps it would avoid confusion and emails if the
> Class::DBI docs
> explained that has_a and has_many are for defining
> both sides of a
> one-to-many relationship, and might_have is for
> defining a one-to-one
> relationship? I know this is obvious to any
> intelligent person who has
> had to go through the implementation of both at
> least once, but at first
> glance, CDBI is concept-rich and thefore
> overwhelming.
>
> Sticking a version of this at the top of the "TABLE
> RELATIONSHIPS"
> section as part of the introduction would probably
> make the whole thing
> go down quicker and smoother, like a spoonful of
> sugar:
>
> ---
> Class::DBI directly supports one-to-one and
> one-to-many relationships,
> and indirectly supports many-to-many relationship.
> To define a
> one-to-one relationship, use the 'might_have' method
> from the dominant
> table (the one you would typically put on the left
> side of a LEFT
> JOIN). To define a one-to-many relationship, use
> the 'has_a' and
> 'has_many' methods, each from the appropriate table.
> If you have an
> 'account' table that has a one-to-many relationship
> with a
> 'monthly_statement' table, you would do something
> like this:
>
> package Account;
> __PACKAGE__->has_many( monthly_statements =>
> 'MonthlyStatement' ); #
> adds monthly_statements method
> package MonthlyStatement;
> __PACKAGE__->has_a( account_id => 'Account' ); #
> 'inflates' account_id
> value into an Account object
> ---
>
> I'm still new, so I'll let someone else right the
> many-to-many intro
> paragraph. :)
>
> -ofer
That makes a lot of sense - however, doesn't it also
say using has_a on the many side of the relationship
(eg. the MonthlyStatement part) is dangerous if you
are trying to inflate a foreign key which also happens
to be a primary key on the many table. Thus look at
using might_have instead. That's how I read the
current documentation - which is why I was having so
many troubles.
Is there a definitive answer to that conundrum?
>joss
___________________________________________________________
ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com
|
(message missing)
|