[Dbix-class] Relationship Bug
Matt Hilliard
mhilliard at strathcom.com
Mon Dec 4 20:39:08 GMT 2006
On Sat, 2006-02-12 at 13:41 +0000, Ash Berlin wrote:
> On 12/2/2006, "Matt Hilliard" <mhilliard at strathcom.com> wrote:
>
> >I believe I've come across a bug in BRICAS/DBIx-Class-0.07003.tar.gz,
> >the synopsis is this:
> >I'm running from a Catalyst context, I start with a DBIx::Class with 2
> >separate foreign keys to the same foreign table, both initially "NULL",
> >for example:
> >
> >package DB::Schema::Model;
> >
> >use strict;
> >use warnings;
> >
> >use base 'DBIx::Class';
> >
> >__PACKAGE__->load_components("PK::Auto", "Core");
> >__PACKAGE__->table("some_table_or_other");
> >__PACKAGE__->add_columns(
> > "id",
> > { data_type => "INT", default_value => undef, is_nullable => 0, size
> >=> 11 },
> > "assorted_other_data",
> > { data_type => "INT", default_value => 0, is_nullable => 0, size =>
> >11 },
> > "curr_address",
> > { data_type => "INT", default_value => undef, is_nullable => 1, size
> >=> 11 },
> > "prev_address",
> > { data_type => "INT", default_value => undef, is_nullable => 1, size
> >=> 11 },
> >);
> >__PACKAGE__->set_primary_key("id");
> >
> >__PACKAGE__->might_have("curr_address", "Addresses", { "foreign.id" =>
> >"self.curr_address" });
> >__PACKAGE__->might_have("prev_address", "Addresses", { "foreign.id" =>
> >"self.prev_address" });
> >1;
> >
> >and I then call find_or_new_related() separately for each relationship,
> >with some processing, quite likely on different Catalyst action calls
> >for example:
> >my $curr = $model->find_or_new_related('curr_address', {});
> ># do some processing,
> ># insert or update new values for $curr here and then persist
> >$curr->insert_or_update;
> >
> ># if $curr is new (which it is in this example) it will have a new id
> ># shove it back into $model
> >$model->update_from_related( 'curr_address', $curr );
> >
> >The bug manifests itself in this way:
> >when I call
> >my $prev = $model->prev_address
> >
> >I get undef for $prev (which I should) however, if I call
> >$prev = $model->find_or_new_related('prev_address', {});
> >
> >I get the $curr entry for $prev, (which I should not).
> >
> >This really looks like a bug to me, but if its something more like a
> >configuration issue, can I please get some insight?
>
> You asked it to _find_ you the related row. Does address have a user ID
> in it? But anway, you've just asked it to find any row.
No. Apparently, I should be using the relationship "has_many" not
"might_have". Regardless, using the "has_many" relationship exhibits
the same broken behaviour.
While it appears now the behaviour of what I've asked may default to
find *any* row, the method is called "find_related", not
"find_unrelated_from_table_with_zero_or_more_related_elements", but I'm
concerned with working code, not poor naming semantics. Specifying the
id as you suggest works well enough, and I'm content to leave it at
that.
Thank You!
>
> But anyway, try the following
>
> $model->find_or_new_related('prev_address', { id =>
> $model->get_column('prev_address') } );
>
> Ash
More information about the Dbix-class
mailing list