December 2013 Archives

Relationships with optional foreign key values (DBIx::Class)

This entry demonstrates how to implement a relationship with optional foreign key values using DBIx::Class.

Imagine two related tables 'human' and 'cat': A cat belongs to a human but when the human dies the cat can continue to roam freely.

package MyApp::Schema::Result::Human;
use strict;
use warnings;
use base 'DBIx::Class';
__PACKAGE__->load_components(qw/ Core /);
    id => {
        data_type         => 'integer',
        is_auto_increment => 1,
        is_numeric        => 1,

About davewood

user-pic I like Toast.