Retrieving column metadata with DBIx::Class

DBIx::Class is a flexible SQL to OO mapper which I use in most of my projects. No more writing and fiddling around with SQL statements. But its flexible enough, so you can get down and dirty with SQL if you need to.

Here’s how to retrieve column metadata from the underlying database schema.

Lets say I need to get column info for the column artist in table song. Assuming you have already set up your Schema modules, this is what you can do:

my $column_ref
    = $schema->resultset('Song')->result_source->column_info('artist');

The $column_ref hashref after the above code is executed:

$VAR1 = {
          'data_type' => 'VARCHAR',
          'default_value' => '',
          'is_nullable' => 0,
          'size' => '75'
};

You can retrieve all columns of a table with the following code:

my @columns
    = $schema->resultset('Song')->result_source->columns;

@columns now contains a list of the column names of the song table.

Leave a Reply