[Fwd: Re: Many-to-Many with just on table plus linking table]

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

From: Jay Strauss
Subject: [Fwd: Re: Many-to-Many with just on table plus linking table]
Date: 20:41 on 04 Apr 2005
Brett Sanger wrote:
> I decided to finally look into Class::DBI, and I'm already quite lost.  The
> documentation is a bit spotty and chaotically mixes low-level and high-level
> bits.  I'll happily offer some patches, but first I need to figure out what's
> going on myself.  (The wiki did not offer clarification on this matter)
> 
> I have a table of data.  I have a second table that lists relationships
> between items in the first table.
> 
> For example, the data table may hold 10 items.  The relationship table
> may list:
> 1 2
> 1 3
> 1 4
> 5 6
> 5 7
> 5 8
> 
> (both columns refer to the same primary key of the data table)
> 
> Following the Actor/Film/Role example from the docs, I tried:
> 
> package Forum::DBI;
> use base 'Class::DBI::SQLite';
> use DBI;
> 
> Forum::DBI->connection('dbi:SQLite:dbname=/var/db/site.db', '', '');
> 
> #########################################
> package Forum::Topic;
> use base 'Forum::DBI';
> 
> Forum::Topic->table('forum_relations');
> Forum::Topic->columns(Primary => qw/message topic/);
> Forum::Topic->has_a(message => 'Forum::Message');
> Forum::Topic->has_a(topic => 'Forum::Message');
> 
> #########################################
> 
> 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']);
> Forum::Message->has_many(topics => [Forum::Topic => 'topic']);
> 
> And the code:
> 
> my $topic = Knight::Forum::Message->create({title => 'Topic 1', body => 'Foo'});
> my $message = Knight::Forum::Message->create({title => 'msg 1', body => 'Bar'});
> $message->add_to_topics({
> 	topic => $topic,
> });
> 
> But I get:
> Can't create Forum::Topic object with null primary key columns () at /usr/local/share/perl/5.8.4/Class/DBI/Relationship/HasMany.pm line 89
> 
> 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.  If I try to be explicit and list both values, it
> ignores what I passed.
> 
> add_to_* is only barely defined in the documentation, so I've been unsuccessful
> in determing what I've done wrong.  Given that the code runs if I meet the
> expectations, that makes it out to be a logical error on my part, but I can't
> see how my sample is significantly different from the Film/Actor/Role example
> in the docs.  
> 
> What am I missing?
> 
> Thanks in advance.

It could be that you are passing an array ref in your has_many

> Forum::Message->has_many(messages => [Forum::Topic => 'message']);
> Forum::Message->has_many(topics => [Forum::Topic => 'topic']);

Try

Forum::Message->has_many(messages => Forum::Topic => 'message');
Forum::Message->has_many(topics => Forum::Topic => 'topic');


(message missing)

[Fwd: Re: Many-to-Many with just on table plus linking table]
Jay Strauss 20:41 on 04 Apr 2005

Generated at 09:29 on 27 Apr 2005 by mariachi v0.52