The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


DBIx::Class::Manual::Example - Simple CD database example


This tutorial will guide you through the process of setting up and testing a very basic CD database using SQLite, with DBIx::Class::Schema as the database frontend.

The database structure is based on the following rules:

  An artist can have many cds, and each cd belongs to just one artist.
  A cd can have many tracks, and each track belongs to just one cd.

The database is implemented with the following:

  table 'artist' with columns:  artistid, name
  table 'cd'     with columns:  cdid, artistid, title, year
  table 'track'  with columns:  trackid, cdid, title

Each of the table's first columns is the primary key; any subsequent keys are foreign keys.


You'll need to install DBIx::Class via CPAN, and you'll also need to install sqlite3 (not sqlite) if it's not already intalled.

The database/tables/data

Your distribution already comes with a pre-filled SQLite database examples/Schema/db/example.db. You can see it by e.g.

  cpanm --look DBIx::Class

If for some reason the file is unreadable on your system, you can recreate it as follows:

  cp -a <unpacked-DBIC-tarball>/examples/Schema dbicapp
  cd dbicapp
  rm db/example.db
  sqlite3 db/example.db < db/example.sql

Testing the database

Enter the example Schema directory

  cd <unpacked-DBIC-tarball>/examples/Schema

Run the script, which will test that the database has successfully been filled.

When this script is run, it should output the following:

 Leave Me Alone
 Smooth Criminal
 Dirty Diana

 get_tracks_by_artist(Michael Jackson):
 Billie Jean (from the CD 'Thriller')
 Beat It (from the CD 'Thriller')
 Leave Me Alone (from the CD 'Bad')
 Smooth Criminal (from the CD 'Bad')
 Dirty Diana (from the CD 'Bad')

 The Marshall Mathers LP has the track 'Stan'.

 get_cds_by_artist(Michael Jackson):

 get_artist_by_track(Dirty Diana):
 Michael Jackson recorded the track 'Dirty Diana'.

 get_artist_by_cd(The Marshall Mathers LP):
 Eminem recorded the CD 'The Marshall Mathers LP'.

Discussion about the results

The data model defined in this example has an artist with multiple CDs, and a CD with multiple tracks; thus, it's simple to traverse from a track back to a CD, and from there back to an artist. This is demonstrated in the get_tracks_by_artist routine, where we easily walk from the individual track back to the title of the CD that the track came from ($track->cd->title).

Note also that in the get_tracks_by_cd and get_tracks_by_artist routines, the result set is called multiple times with the 'next' iterator. In contrast, get_cd_by_track uses the 'first' result set method, since only one CD is expected to have a specific track.

This example uses "load_namespaces" in DBIx::Class::Schema to load in the appropriate Result classes from the MyApp::Schema::Result namespace, and any required ResultSet classes from the MyApp::Schema::ResultSet namespace (although we did not add, nor needed any such classes in this example).


Check the list of additional DBIC resources.


This module is free software copyright by the DBIx::Class (DBIC) authors. You can redistribute it and/or modify it under the same terms as the DBIx::Class library.