[CDBI] essential fields not populating as expected

Jon Burdge jon at burdge.org
Tue May 1 00:22:43 BST 2007

Class::DBI is not loading essential fields for objects in some circumstances
where the information is available.  I think this is a side-effect of the
uniqueness feature.  I'm trying to determine how to work around this.

I hit this problem in some code that was using sth_to_objects to preload a
bunch of objects I knew I'd be using shortly (as an optimization).  I think,
though, that I have a simpler example that demonstrates the same problem:

create table a (
  aid integer primary key,
  somedata varchar(50)
create table b (
  bid integer primary key,
  a integer,
  otherdata varchar(50)
insert into a (aid, somedata) values (1, 'blah');
insert into b (bid, a, otherdata) values (1, 1, 'foo');

package Example;
use base 'Class::DBI';
    "DBI:Pg:dbname=example;host=localhost;port=5432", "postgres", "");

package Example::A;
use base 'Example';
__PACKAGE__->columns(Essential => qw/aid somedata/);
__PACKAGE__->has_many(b => 'Example::B');

package Example::B;
use base 'Example';
__PACKAGE__->columns(Essential => qw/bid a otherdata/);
__PACKAGE__->has_a(a => 'Example::A');

package main;
use Data::Dumper;
  my $a = Example::A->retrieve(1);
  print Dumper($a);
my $b = Example::B->retrieve(1);
my $a = Example::A->retrieve(1);
print Dumper($a);

The problem is that the second time $a is dumped, the somedata field is not
loaded.  This looks like the same problem I'm having with sth_to_objects--I
have some objects already created that reference another table, and then I
try to load the data for that other table, but the information from the
statement handle passed to sth_to_objects seems to be getting ignored in
favor of the stub object already in memory.

I think the issue is essentially happening in these lines in _init:

    my $key   = $class->_live_object_key($data);
    return $Live_Objects{$key} || $class->_fresh_init($key => $data);

Does anyone have a suggestion on the appropriate workaround for this
situation before I go mucking around with _init?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digitalcraftsmen.net/pipermail/classdbi/attachments/20070430/eeceed6a/attachment.html

More information about the ClassDBI mailing list