[Dbix-class] Populating Instance With Column's Defaults

Peter Rabbitson rabbit+dbic at rabbit.us
Wed Feb 9 07:37:24 GMT 2011


Skye Shaw wrote:
> Hello,
> 
> Is it possible to have DBIC populate a newly created instance with the
> underlying table's defaults when a value isn't provided?
> 
> Given my code (active defaults to 1):
> 
> my $user = $db->resultset('Account')->create({
>   first_name =>'A',
>   last_name => 'User'
> });
> 
> print $user->active; #undef warning
> $user = $db->resultset('Account')->find($user->id);
> print $user->active; #now it's populated with 1
> 
> And this thread:
> http://stackoverflow.com/questions/2106504/perl-dbixclass-default-values-when-using-new
> 
> The answer appears to be no. If so, what's the reason for this behavior?
> 

The reason is that having every insert be followed by a select is...
how shall I put it, insane?! What you want is the (admittedly misnamed)
http://search.cpan.org/~abraxxa/DBIx-Class-0.08127/lib/DBIx/Class/Row.pm#discard_changes_%28$attrs?%29

Note that frew would be very happy if you contribute an "auto-retrieve
defaults" component to DBIx::Class::Helpers



More information about the DBIx-Class mailing list