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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |