I have a DBIx::Class::Schema built by DBIx::Class::Schema::Loader, and I'm starting to play around with it.

I have a table with a default value for a column (this is an example, not real data):

CREATE TABLE t ( i int DEFAULT 3 NOT NULL, x varchar(255) );

My class knows about this.

__PACKAGE__->load_components( 'PK::Auto', 'Core' ); __PACKAGE__->table('t'); __PACKAGE__->add_columns( 'i', { data_type => 'integer', default_value => 3, is_nullable => 0, size => 4, }, 'x', { data_type => 'character varying', default_value => undef, is_nullable => 1, size => 255, }, );

However, according to DBIx::Class::ResultSource, the default_value is only used by "deploy" in DBIx::Class::Schema. Fine.

The behavior I'm seeing is that if I create a new row, it doesn't have the default value.

my $rs = $schema->resultset( 'T' ); isa_ok( $rs, 'DBIx::Class::ResultSet' ); my $row = $rs->create( { x => 'foo' } ); isa_ok( $row, 'DBIx::Class::Row' ); ok( $row->in_storage(), 'new row is in storage' ); # THIS IS THE TEST THAT FAILS is( $row->i(), 3, 'new row has default value' );

I've done a few laps around the documentation and not found how to handle this.

Ideally, it would know that the data it has doesn't match what's actually in storage. I'd like it to automatically go back to the database and see what's there.

Since I know I left a value to its default, I could undef $row and make a new one with a search for the record I just created. I was hoping, however, that there's a way I could tell the $row, "you're not current". This would be useful also in a case where I might modify something directly with SQL without using the DBIx::Class-based model. Is there such a thing?


In reply to Default column values in DBIx::Class by kyle

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.