[Dbix-class] Self-Reference table configuration issue

Matt S Trout dbix-class at trout.me.uk
Wed Oct 18 22:58:48 CEST 2006


Hill, Woodrow J wrote:
> Greetings,
>  
>     I'm in the process of converting over to DBIx::Class from
> Class::DBI, and having issues with a setup that I have functional in the
> latter.  I have two tables in the database, one a "cross-reference" to
> the other table, which references itself to define a hierarchical
> structure.  I'm having some issues adjusting the "many-to-many"
> documentation to query such a setup correctly.  The fundamental error is
> that, when I call on an item that's supposed to be higher or lower in
> the hierarchy, I get back my originating item.
> 
> My setup, trimmed down for public posting purposes, is very similar to
> this:
> 
> [CrossReference table]
> CrossRefID
> HigherInformation (foreign key for SelfReference item higher in
> hierarchy)
> LowerInformation (foreign key for SelfReference item lower in hierarchy)
> 
> [SelfReference table]
> InformationID
> InformationTitle
> 
> [CrossReference.pm : Module for cross-reference table]
> [...]
> __PACKAGE__->belongs_to(HigherInformation	=>
> 'Project::SelfReference');
> __PACKAGE__->belongs_to(LowerInformation	=>
> 'Project::SelfReference', 'LowerInformation');
> 
> [SelfReference.pm : Module for self-referenced table]
> [...]
> __PACKAGE__->has_many(higher_xref	=> 'Project::CrossReference',
> 'HigherInformation');
> __PACKAGE__->has_many(lower_xref	=> 'Project::CrossReference',
> 'LowerInformation');

I think these two lines are wrong, and you need to swap the third args.

If you think about it, if you're referring to *lower* info than yourself, then 
logically you want to be *higher* thing in the link table, and vice versa.

> __PACKAGE__->many_to_many(higher_info	=> 'higher_xref',
> 'HigherInformation');
> __PACKAGE__->many_to_many(lower_info	=> 'lower_xref',
> 'LowerInformation');
> 
> [From my test code]
> [...set up Schema connection...]
> my $test_information_item =
> $schema->resultset('Information')->find(750);
> is($test_information_item->lower_info->next->InformationID, '3542'); #
> comes out as 750
> 
> 
> As I mentioned, the Class::DBI equivalent I created works OK, if slowly
> -- indeed, early benchmarking of simpler queries was a major reason I've
> attempted this changeover.  In the hope that I'm just missing a bit of
> basic setup logic from DBIx::Class, as opposed to a syntactical error,
> I've posted the above; if people want/need to see complete code, please
> let me know.

Nah, you just got the definitions swapped. I'm good at that as well, about 3/4 
of my many-many problems tend to turn out to be I got one of the args the 
wrong way round.

Declaration syntax that will be proof against us being silly buggers is in the 
works :)

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list