[Dbix-class] Relations on non in_storage rows (my todays gotcha)

Zbigniew Lukasiak zzbbyy at gmail.com
Sun May 11 17:48:03 BST 2008


On Sun, May 11, 2008 at 4:34 PM, Matt S Trout <dbix-class at trout.me.uk> wrote:
> On Sun, May 11, 2008 at 12:06:54PM +0200, Zbigniew Lukasiak wrote:
>> If you have a new object, not yet in the database, like:
>>
>> $new_cd = $schema->resultset("CD")->new( {} );
>>
>> Then if you call $new_cd->artist - this will return a first artist in
>> the 'artist' table.
>
> That's a bug, but the test we were given for it didn't work. Better test
> case welcome and we can fix it.

There is a strange test in 66relationship.t:

my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title'
=> 'badgers', 'year' => 2007 });
is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded');
is($undef_artist_cd->search_related('artist')->count, 3, 'open search
on undef FK');

So it tests that a new row has 3 related artists - I cannot understand
what that is supposed to mean.  I my opinion that count there should
be 0.

If we need another test then for example:

my $new_cd = $schema->resultset("CD")->new( {} );
is( $new_cd->search_related( 'artist' )->first, undef, 'No artist
found for new cd' );

Maybe I don't understand what is oing on here - but if my thinking is
right and the test 'open search on undef FK' is wrong than the fix
could be to change line 796 in lib/DBIx/Class/ResultSource.pm:

-        $ret{$k} = $for->get_column($v) if $for->has_column_loaded($v);
+        $ret{$k} = $for->get_column($v);

In my opinion that would be more 'intuitive' - I don't care if the
column is loaded from the disk or if I set it manually - if it is set
then I would like that the right object is related.

--
Zbigniew

>
> --
>      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
>   Technical Director                    http://www.shadowcat.co.uk/catalyst/
>  Shadowcat Systems Ltd.  Want a managed development or deployment platform?
> http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.rawmode.org
>



-- 
Zbigniew Lukasiak
http://brudnopis.blogspot.com/



More information about the DBIx-Class mailing list