zby has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple script:
use lib 'lib'; use Party::M::CDBI::Declaration; my $a = Party::M::CDBI::Declaration->create({freetext => 'aaasdds'});
The primary key column is of the 'serial' type so it should be set from the implicite sequence here - but still I get following error:
Can't insert new Party::M::CDBI::Declaration: DBD::Pg::st execute fail +ed: ERROR: null value in column "id" violates not-null constraint [for Statement "INSERT INTO declaration (freetext, id) VALUES (?, ?) " with ParamValues: ] at /usr/local/share/perl/5.8.4/DBIx/ContextualFe +tch.pm line 51. at a.pl line 6
Interestingly when I run in psql I omit the 'id' in the values the statement works:
INSERT INTO declaration (freetext) VALUES ('aaasdds');
Here is the the rest of the relevant code:
package Party::M::CDBI; use strict; use base 'Class::DBI'; __PACKAGE__->connection('dbi:Pg:dbname=party', 'zby', '***', { AutoCom +mit => 1 , RaiseError=>1}); package Party::M::CDBI::Declaration; use base Party::M::CDBI; use strict; __PACKAGE__->table('declaration'); __PACKAGE__->columns(All => qw/id usr place freetext cdate/); __PACKAGE__->has_a(usr => 'Party::M::CDBI::Usr');
create table declaration( id serial primary key, usr integer CONSTRAINT declaration_usr references usr(id) on delete ca +scade, place varchar(50), freetext text, cdate timestamp default CURRENT_TIMESTAMP );

Replies are listed 'Best First'.
Re: Class::DBI and 'serial' columns in PostgreSQL
by davidrw (Prior) on Aug 16, 2005 at 16:49 UTC
    It seems that it is doing INSERT INTO declaration (freetext,id) VALUES ('aaasdds',null); ... What if you add:
    __PACKAGE__->columns(Primary => 'id'); # y, i know it implictly use +s the first column __PACKAGE__->sequence('declaration_id_seq');
    (An educated guess based on the example at Class::DBI::Pg)
      Thanks! __PACKAGE__->sequence('declaration_id_seq'); did the trick. The primary key was deduced all right.