Resurrecting this thread, because I have also been struggling with this of late, and I thought it would be a good idea to post what I have learnt.

I found that to make things work, I needed to put the table classes before the main schema class, in the source file, and that I can't have any extra levels in the class hierarchy between the Schema class and the table classes. (Not sure why)

Here is an example script that I got working

#!/usr/bin/perl # Test/Demo of a single file perl script using DBIx::Class. # Notes on how to make this work: # The Table class(es) need to appear before the Schema class or # the rest of the program. # The package of the table classes can't have any extra levels # (eg <schema>::Result::<table> won't work. # Remember the deploy command if the initial DB is empty. # Put the DBIx::Class packages in the same file package LocalTest::Schema::TestTable; use base 'DBIx::Class::Core'; use DateTime; __PACKAGE__->table('tblTest'); __PACKAGE__->load_components(qw/InflateColumn::DateTime/); __PACKAGE__->add_columns( 'id' => { data_type=>'int', is_auto_increment=>1 }, 'date' => { data_type=>'datetime' }, 'text' => { data_type=>'varchar', size=>20, is_nullable=>1 }, ); __PACKAGE__->set_primary_key('id'); sub new { my( $class, $arg ) = @_; $arg->{date} ||= DateTime->now; return $class->next::method( $arg ); } package LocalTest::Schema; use base 'DBIx::Class::Schema'; __PACKAGE__->load_namespaces; __PACKAGE__->load_classes('TestTable'); # Back to the main program package main; use strict; use warnings; use diagnostics; use English; use DateTime; # use LocalTest::Schema; # This import does not work, & not needed. my $schema = LocalTest::Schema->connect('dbi:SQLite:dbname=:memory:'); $schema->deploy(); my $row1 = $schema->resultset('TestTable')->create({'text'=>'foo_bar'} +); my $row2 = $schema->resultset('TestTable')->create({}); my $row3 = $schema->resultset('TestTable')->create({'text'=>'frodo'}); my $row_rs = $schema->resultset('TestTable')->search({'id'=>[1,2]}); my @rows = $row_rs->all; printf "Found %d rows\n", $row_rs->count;

In reply to Re: DBIx::Class in a single file by chrestomanci
in thread DBIx::Class in a single file by rppowell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.