[CDBI] Re: has_a and null foreign keys

Edward J. Sabol sabol at alderaan.gsfc.nasa.gov
Tue Apr 10 18:59:42 BST 2007

Arshavir Grigorian wrote:
> I have 2 tables that are related to each other through has_a
> relationship in Class::DBI. However, it can happen that a record in
> table1 does not have the foreign key for table2 populated. In this
> situation, if I try to call My::Class->retrieve(id =>
> $id)->foreign_key->column_in_table2(), I get an error because
> foreign_key() didn't return a valid object and column_in_table2()
> fails. Should I be trying to check whether
> foreign_key() returns a valid object and only then call column_in_table2()
> or is there another way?

Technically, you have a might_have() relationship, but there's no reason why
you can't use has_a() really, if that's your preference. The short answer to
your question is yes; you need to check whether ->foreign_key() is a valid
object (defined() should be sufficient) before calling the
->column_in_table2() method.

Hope this helps,

More information about the ClassDBI mailing list