http://www.perlmonks.org?node_id=552424

BrentDax has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a Catalyst app using DBIx::Class accessing MySQL for my model. Two of my tables have a many-to-many relationship, and I'm having trouble constructing the right query for the problem at hand.

Okay, so I have the classic many-to-many relationship: an articles table, a tags table, and a links table with article and tag IDs. I'm using DBIx::Class to access these tables, and I have the relationships set up the way you would expect:

schema::articles->has_many(taglinks => 'schema::links', 'article'); schema::links->belongs_to(article => 'schema::articles'); schema::links->belongs_to(tag => 'schema::tags'); schema::tags->has_many(articlelinks => 'links', 'tag'); schema::articles->many_to_many(tags => taglinks => 'tag'); schema::tags->many_to_many(articles => articlelinks => 'article');

Now, at one point in my code I have two arrays with rows from the tags table. I want to find the articles with all of the tag rows in @with and none of the tag rows in @without. The articles can have tags that aren't in either array. (Oh, and to complicate things further, I may want to exclude articles which don't have certain values in columns of the articles table itself, but I'll burn that bridge when I get to it.) How do I do this?

My searching turned up Many-to-many relationships in databases : SOLVED, but I'm not sure how to apply this to DBIx::Class, or if it's even the right approach for this module. Any help would be appreciated.

Thanks,

=cut
--Brent Dax
There is no sig.