Re: Many-to-Many with just on table plus linking table

[prev] [thread] [next] [Date index for 2005/04/04]

From: Perrin Harkins
Subject: Re: Many-to-Many with just on table plus linking table
Date: 21:25 on 04 Apr 2005
On Mon, 2005-04-04 at 15:03 -0400, Brett Sanger wrote:
> I have a table of data.  I have a second table that lists relationships
> between items in the first table.

Kind of confusing, since this looks like it's supposed to be a tree, but
the way you have it modeled in your database it may not be one.

> package Forum::Topic;
> use base 'Forum::DBI';
> 
> Forum::Topic->table('forum_relations');

Maybe Forum::Relation would be a clearer class name here.  A topic is
actually another message, isn't it?

> It turns out that add_to_topics() wants a 'message' value passed, and
> add_to_messages wants a 'topic' value passed, with the result of switching the
> meanings of my columns.

Your naming conventions are kind of making my head spin, and may be
screwing up the auto-guessing that CDBI does to figure out which column
in the foreign class you make the has_many link to is referencing this
class.  Try using the explicit form with a third argument:

package Forum::Message;
use base 'Forum::DBI';

Forum::Message->table('forum_posts');
Forum::Message->columns(All => qw(id title body));
Forum::Message->has_many(messages => [Forum::Topic => 'message'],
'topic');
Forum::Message->has_many(topics => [Forum::Topic => 'topic'],
'message');

This says that a message has topics and a topic has messages.

- Perrin

Re: Many-to-Many with just on table plus linking table
Perrin Harkins 21:25 on 04 Apr 2005

Generated at 14:42 on 11 Apr 2005 by mariachi v0.52