[Dbix-class] Problem with automatic primary keys
Robert Norris
rob at cataclysm.cx
Wed Jun 14 02:48:58 CEST 2006
So I'm trying to get DBIx::Class working for a new app. I've never used
it (or any other ORM) before, so please be kind if this has an obvious
answer.
$DBIx::Class::VERSION = 0.06999_02;
$DBI::VERSION = 1.50;
$DBD::Pg::VERSION = 1.49;
This is part of my Postgres setup:
create sequence e_group_pk;
create table e_group (
groupid integer not null primary key,
name text not null
);
I have this in my DBIC schema class to match:
package Emma::Schema::Group;
use base qw(DBIx::Class);
__PACKAGE__->load_components(qw(PK::Auto Core));
__PACKAGE__->table("e_group");
__PACKAGE__->add_columns(qw(groupid name));
__PACKAGE__->set_primary_key("groupid");
__PACKAGE__->sequence("e_group_pk");
Running this:
my $group = $s->resultset("Group")->create({ name => "admin" });
$group->update;
Gets me this error:
DBD::Pg::st execute failed: ERROR: null value in column "groupid" violates not-null constraint
DBIx::Class::ResultSet::create(): Error executing 'INSERT INTO e_group (name) VALUES (?)': ERROR: null value in column "groupid" violates not-null constraint
My understanding is that PK::Auto will make it setup groupid
automatically by getting a new value from the sequence. Is that right?
Database log shows that the sequence never gets queried:
LOG: statement: begin; select getdatabaseencoding(); commit
LOG: statement: SELECT version(), 'DBD::Pg'
LOG: statement: SELECT 'DBD::Pg ping test'
LOG: statement: begin
LOG: statement: INSERT INTO e_group (name) VALUES ('admin')
ERROR: null value in column "groupid" violates not-null constraint
LOG: statement: SELECT 'DBD::Pg ping test'
ERROR: current transaction is aborted, commands ignored until end of
transaction block
LOG: statement: rollback
Gotta say that I'm quite impressed so far. My database fu is pretty
weak, and I've spent the last couple of years manually building poorly
designed databases and access objects. Meanwhile, it took me about four
hours to read the DBIC docs, define a really nicely structured database
and get the schema class done. And I didn't want to stop when I'd done.
So very nice work guys, you've convinced me :)
Cheers,
Rob.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20060614/b8c75751/attachment.pgp
More information about the Dbix-class
mailing list