Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Default column values in DBIx::Class

by jasonk (Parson)
on Jan 12, 2008 at 03:29 UTC ( [id://662023]=note: print w/replies, xml ) Need Help??


in reply to Default column values in DBIx::Class

To tell the object "you're not current" and have it reloaded from the database:

$row->discard_changes;

To set the default value from your DBIx::Class object instead of from the database, and avoid the whole problem...

package MyDB::T; use base qw( DBIx::Class ); # setup stuff ... sub new { my ( $class, $attrs ) = @_; $attrs->{ 'i' } = 3 unless defined $attrs->{ 'i' }; return $class->next::method( $attrs ); }

We're not surrounded, we're in a target-rich environment!

Replies are listed 'Best First'.
Re^2: Default column values in DBIx::Class
by kyle (Abbot) on Jan 12, 2008 at 03:51 UTC

    Thank you!!

    So as not to have my constants in two places, I've written something like this:

    sub new { my ( $class, $attr ) = @_; foreach my $col ( $class->columns ) { my $col_info = $class->column_info( $col ); if ( ! defined $attr->{$col} && exists $col_info->{default_value} && ! $col_info->{is_auto_increment} ) { $attr->{$col} = $col_info->{default_value}; } } return $class->next::method( $attr ); }

    With the little testing that I've done so far, this seems to work like a charm.

    Thanks again!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://662023]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found