[Dbix-class] postgresql and serial primary key

Luca Ferrari fluca1978 at infinito.it
Fri Feb 6 13:08:52 GMT 2015


On Mon, Feb 2, 2015 at 8:32 PM, Maddy, Noel <NMADDY1 at hfhs.org> wrote:
> Luca,
> IIRC, if you pass any value (even NULL) for the pk column, then it bypasses the sequence.
> You'll need to drop the pk column completely when $pk is undefined.
> I'd just use PerlX::Maybe, like this:
> my $current_ticket = $c->model('DB::Ticket')->find_or_new( maybe pk => $pk );

In my case I use a less elegant technique building the hash ref to
find_or_new before invoking it, so that I can add the pk or not:

$params_ref = {};
$params_ref->{pk} = $pk if ( $pk );
my $current_ticket = $c->model('DB::Ticket')->find_or_new( $params_ref );

Another approach could have been to call separately find or new with a
couple of conditionals:

my $model = $c->model('DB::Ticket');
my $current_ticket = ( $pk ? $model->find ({pk => $pk}) : $model->new() );


More information about the DBIx-Class mailing list