[Dbix-class] multi-way relationships best practise

Matt S Trout dbix-class at trout.me.uk
Fri Mar 3 13:41:47 CET 2006


On Fri, Mar 03, 2006 at 11:58:04AM +0000, Carl Franks wrote:
> I have a table of events, and another table of parent relationships, as below.
> 
> package cdiary::Schema::Event;
> 
> __PACKAGE__->table('events');
> 
> __PACKAGE__->add_columns(qw/ id title /);
> 
> __PACKAGE__->set_primary_key('id');
> 
> __PACKAGE__->might_have( parent => 'Event_rel', {'foreign.id' => 'self.id'} );
> 
> 
> package cdiary::Schema::Event_rel;
> 
> __PACKAGE__->table('event_rel');
> 
> __PACKAGE__->add_columns(qw/ id parentid /);
> 
> __PACKAGE__->set_primary_key('id');
> 
> __PACKAGE__->belongs_to( parent => 'Event', { 'foreign.id' =>
> 'self.parentid' } );
> 
> 
> An event may have an entry in the 'rel' table, stating which other
> event is it's parent.
> (The purpose is for repeat appointments)
> 
> What I'd like to know is, will it cause me any problems from a
> DBIx-Class point-of-view if I add another relationship to the event
> schema, like so:
> 
> 
> __PACKAGE__->might_have( child => 'Event_rel', {'foreign.parentid' =>
> 'self.id'} );
> 
> So it has both might_have(child) and might_have(parent)
> 
> Will DBIx-Class choke when I start adding/deleting relationships?

If it does, send us a test case :)

Twisty recursive/self relationships working smoothly is something we've
put a fair amount of thought into; have a look at the SelfRef/SelfRefAlias,
ArtistUndirectedMap and TreeLike classes in the DBICTest schema for a few
examples.

-- 
     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