Re: Foreign key as primary key...

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

From: Ofer Nave
Subject: Re: Foreign key as primary key...
Date: 20:26 on 20 Feb 2005
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

(message missing)

Foreign key as primary key...
jossblowing 15:11 on 18 Feb 2005

Re: Foreign key as primary key...
Tony Bowden 16:16 on 18 Feb 2005

Re: Foreign key as primary key...
fdsfds fdsfddsf 13:42 on 20 Feb 2005

Re: Foreign key as primary key...
Tony Bowden 14:48 on 20 Feb 2005

Re: Foreign key as primary key...
Joss Shaw 15:13 on 20 Feb 2005

Re: Foreign key as primary key...
Ofer Nave 20:26 on 20 Feb 2005

Re: Foreign key as primary key...
Joss Shaw 11:22 on 22 Feb 2005

Re: Foreign key as primary key...
Perrin Harkins 18:09 on 22 Feb 2005

Generated at 11:30 on 23 Feb 2005 by mariachi v0.52