[Dbix-class] Difference between belongs_to and has_one

Jess Robinson castaway at desert-island.demon.co.uk
Fri Oct 20 20:52:08 CEST 2006



On Thu, 19 Oct 2006, Drew Taylor wrote:

> One thing I discovered after my post which I think is CRITICAL:
> has_one & might_have both use the PK of the local (what's the proper
> terminology?) table as the FK into the foreign table. Perhaps saying
> something like this for the might_have docs:
>
> "Creates an optional one-to-one relationship with a class, where the
> primary key of the foreign class is equal to the primary key of the
> local table. Ie. Foo.id == Bar.id. Unlike belongs_to, might_have ONLY
> suppports using each tables primary key as the key column."
>
> I'm sure I said that in a very ineloquent way, but hopefully you get the idea.
>

belongs_to and has_one/might_have are complete opposites.. Yes the naming 
of belongs_to is rorrible, I blame mst (who will no doubt blame AR) ..

belongs_to means: a field (or fields) in THIS table is a foreign key 
(contains the primary key of) THAT other table.

has_one means: there is a row in THAT table which contains THIS tables 
primary key.

ditto might_have, except its not guaranteed to be there.

HTH
Jess



More information about the Dbix-class mailing list