[Dbix-class] Null foreign keys and the reload_from_storage error when its not wanted

David Ihnen davidi at norchemlab.com
Fri May 22 01:27:45 GMT 2009


Is this appropriate behavior or a bug?

Consider this code here.  It is part of our application's database test 
script.  Its purpose is to hit every rel defined in the database so that 
we can get an early warning on any class configuration mismatching the 
state of the database.

# get all the sources
my $sources = $user->result_source->schema->{'source_registrations'};
# grab all the registered sources
foreach my $source (keys %{$sources}) {
  # grab all their relationships
  foreach my $rel (keys %{$sources->{$source}->{'_relationships'}}) {
    eval {
      my $row = $DBH->resultset($source)->single;
      $row->$rel if ($row)
    };
    print "$@\n" if ($@);
    ok(!$@, "test rel $source->$rel" );
  }
}

Runnign this eventually prints out this message here

DBIx::Class::Relationship::Accessor::__ANON__(): Column dependency_id 
not loaded or not passed to new() prior to insert() on 
DB::Schema::cron_task=HASH(0x2fa4940) trying to resolve relationship 
(maybe you forgot to call ->reload_from_storage to get defaults from the 
db) at 02_db.t line 40
not ok 563 - test rel cron_task->dependency


Which although I understand the reason for this message, its only 
relevant when doing a new or insert because the database layer might put 
a default value in.  When doing a select (->single in this case) - The 
message still throws whenever we attempt to reference an object for 
which the key is null.  Even if it is 100% legitimately null and was 
just selected as such from the database.  Particularly when the 
relationship was defined as being only a possibility.

__PACKAGE__->might_have( dependency  => 'DB::Schema::cron_task', { 
'foreign.id' => 'self.dependency_id' } );

Since this table has a conditional relation - there is only a value 
there if there IS a dependency on the cron task - its inevitable in 
configuration that there will be null field values in this key field.  
Any attempt to reference through them used to return undefined, (or 
nothing when I was playing with null pattern).  Now it throws this 
exception preemptively.

Is this appropriate behavior or is it a bug?

David


PS: The function is 'get_from_storage', not 'reload_from_storage', right

?



More information about the DBIx-Class mailing list