[Dbix-class] postgresql and serial primary key

Maddy, Noel NMADDY1 at hfhs.org
Mon Feb 2 19:32:05 GMT 2015


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 );

From: Luca Ferrari [fluca1978 at infinito.it]
Sent: Monday, February 02, 2015 12:02 PM
To: DBIx::Class
Subject: [Dbix-class] postgresql and serial primary key

Hi all,
I'm a problem with PostgreSQL serial field used as keys, and it seems
there's something in DBIx I'm not understanding.
My Result has a definition for the column as follows:

    data_type         => "integer",
    is_auto_increment => 1,
    is_nullable       => 0,
    sequence          => "tickets_pk_seq",
  }, ...

when I want to add a new field I do the following (in a catalyst
application) in the controller;

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

the problem is that for a new record the pk field is still null, and
therefore the sequence is not called since the value NULL is placed in
the query.
Moreover, since my Result has has_many objects, the null value is
propagated to the child objects.
Is there a way to auto-query the sequence when the record is going to
be inserted? I don't want to query it always to avoid sequence-holes.
Similarly, I don't want to find_or_create records since the user could
not end inserting it and I would have "blank" tuples.




CONFIDENTIALITY NOTICE: This email contains information from the sender that may be CONFIDENTIAL, LEGALLY PRIVILEGED, PROPRIETARY or otherwise protected from disclosure. This email is intended for use only by the person or entity to whom it is addressed. If you are not the intended recipient, any use, disclosure, copying, distribution, printing, or any action taken in reliance on the contents of this email, is strictly prohibited. If you received this email in error, please contact the sending party by reply email, delete the email from your computer system and shred any paper copies.

Note to Patients: There are a number of risks you should consider before using e-mail to communicate with us. See our Privacy & Security page on www.henryford.com for more detailed information as well as information concerning MyChart, our new patient portal. If you do not believe that our policy gives you the privacy and security protection you need, do not send e-mail or Internet communications to us.

More information about the DBIx-Class mailing list