[Dbix-class] find_or_create problem with Pg: proposed patch

Jess Robinson castaway at desert-island.me.uk
Thu Apr 3 18:55:00 BST 2008



On Thu, 3 Apr 2008, Alex Povolotsky wrote:

> Hello!
>
> Attempt to insert into postgres table with undef's for columns with default 
> values yields an error. It is especially bad with find_or_create sub.

Are those columns also nullable, or not? If they're not, inserting 
null/undef doesn't make sense.. In DBIx::Class or plain SQL. the solution 
is generally to insert \'DEFAULT' ..

Jess

> I've made and tested a simple patch, it looks working ok.
>
> --- DBIx/Class/Storage/DBI/Pg.pm.orig   2007-08-12 01:07:58.000000000 +0400
> +++ DBIx/Class/Storage/DBI/Pg.pm        2008-04-03 19:26:46.000000000 +0400
> @@ -13,6 +13,20 @@
> warn "DBD::Pg 1.49 is strongly recommended"
>  if ($DBD::Pg::VERSION < 1.49);
>
> +sub insert {
> +    my ($self, $source, $to_insert) = @_;
> +    for my $col ( keys %$to_insert ) {
> +       if (!defined($to_insert->{$col})) {
> +           # Possible fix up case
> +           my $colinfo = $source->column_info($col);
> +           if (!$colinfo->{is_nullable} && $colinfo->{default_value}) {
> +               delete $to_insert->{$col};
> +           }
> +       }
> +    }
> +    $self->SUPER::insert($source, $to_insert);
> +}
> +
> sub _dbh_last_insert_id {
>  my ($self, $dbh, $seq) = @_;
>  $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
>
> Alex.
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: 
> http://www.grokbase.com/group/dbix-class@lists.rawmode.org
>



More information about the DBIx-Class mailing list