[Dbix-class] Making DBD::Pg to return Postgres arrays as Perl arrays

Tim Bunce Tim.Bunce at pobox.com
Thu May 7 19:49:22 GMT 2015


On Thu, May 07, 2015 at 01:30:45PM +0200, Adam Sjøgren wrote:
> I wrote:
> 
> > When I insert new rows where the field has a Perl-array as the value,
> > DBIx::Class+DBD::Pg automatically stores them in the database, as hoped
> > - nice!
> 
> > But when I read the field again, I get the textual "Postgres-encoded"
> > representation back (i.e. a string like '{"(a,b,c)","(d,e,f)"}').
> 
> After looking more closely on this:
> 
> Simple arrays work out of the box, i.e. text[] I can read/write and
> values are automatically converted from Perl arrays when writing to the
> database, and converted back to Perl arrays when reading.
> 
> The problem I have is only when my datatype is a (custom) tuple.
> 
> I.e. in Postgres I have:
> 
>   CREATE TYPE entry AS (tag TEXT, created TIMESTAMP, notes TEXT, initials TEXT);
>   ALTER TABLE vehicle ADD COLUMN entrylist entry ARRAY;
> 
> I would like to be able to do:
> 
>   $schema->resultset('Vehicle')->create({ entrylist=>[
>                                                       [ 'a', '2015-05-07', 'info', 'adsj' ],
>                                                       [ 'b', '2015-05-07', 'more', 'adsj' ],
>                                                      ] });

Take a look at https://metacpan.org/pod/PGObject::Type::Composite

Tim.



More information about the DBIx-Class mailing list