Tuesday, August 24, 2010

Dynamically Adding Columns with DBIx::Class

I have a DBIx::Class object representing an eBay auction. The underlying table has a description column which contains a lot of data. The column is almost never used, so it's not included in the DBIx::Class row object definition. I do, however, have one script that needs this column. I used to have code like
my $rs = $schema->resultset('Lots');
my $lots = $rs->search(
   { bar => undef },
   { '+select' => 'description', '+as' => 'description' },
);
...
$lot->update({ description => '...' });
but after upgrading to DBIx::Class version 0.08123 (from a really old version), it started giving the error
DBIx::Class::Relationship::CascadeActions::update(): No such column description at ...
Since this code runs inside an independent cron job, I can dynamically modify the schema without affecting any of the other scripts or the main website.
my $rs = $schema->resultset('Lots');
$rs->result_source->add_columns('description');
MyApp::Schema::Lots->add_columns('description');
MyApp::Schema::Lots->register_column('description');
my $lots = $rs->search({ bar => undef });
...
$lot->update({ description => '...' });
Since DBIx::Class doesn't support lazily loaded columns, this seems to be the best available workaround at the moment.

0 comments: