[Dbix-class] "regression" introduced in DBIC r5298.. Or we're Doing It Wrong..

Toby Corkindale toby.corkindale at strategicdata.com.au
Thu Jun 4 03:19:47 GMT 2009


Toby Corkindale wrote:
> Hi guys,
> I've encountered a regression in DBIC, introduced in revision 5298.
> It's more than likely that the way things were being done in the code 
> here were not correct and *shouldn't* have worked previously, but I 
> can't seem to work it out myself.
> 
> There's a single changed line which triggers the problem:
> --------------------------------------------------------
> diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm
> @@ -1228,7 +1229,7 @@ sub register_extra_source {
>  sub _register_source {
>    my ($self, $moniker, $source, $params) = @_;
> 
> -  %$source = %{ $source->new( { %$source, source_name => $moniker }) };
> +  $source = $source->new({ %$source, source_name => $moniker });
> 
> --------------------------------------------------------
> 
> I've created a small unit test that demonstrates the issue, here:
> http://dryft.net/dbic/tobyc-dbic-problem.tar.gz
> 
> The failure manifests itself as missing relationships on the result 
> sources. They're clearly setup, and it all works fine until the above 
> line is changed in DBIC/Schema.pm.

Following up on this..
Experimentation shows that if I setup the relationships *before* calling 
register_class() then the relationships stick around.
Setting them up after register_class() means they disappear.

Is that meant to be the way things work?

Currently the relationships are added by calling:
$schema->class('Foo')->has_many(...);

Perhaps this isn't the Right Way to add them?
Is there a better way?

Note that neither of these work:
$schema->source('Foo')->has_many(..);
$schema->resultset('Foo')->result_source->has_many(..);
-- Both fail with "Can't locate object method "has_many" in.."

After some discussion on IRC with Caelum and Ribasushi (thanks!) I ended 
up with the following in the test case I posted:

     for my $source ($schema_namespace->sources) {
         my $class = $schema_namespace->class($source);
         $class->_setup_relationships($schema_namespace);
         # *** This next call added to fix bug ***
         $schema_namespace->register_extra_source(
             $source => $class->new->result_source_instance
         );
     }


This seems to fix the bug in the test case!
Do you think I'm doing this the right way now?

Cheers,
Toby



More information about the DBIx-Class mailing list