I've got something very similar to that working now that doesn't break the DBIx::Class style too much. The SQL is (roughly)

SELECT i.item_id FROM items AS i LEFT JOIN mapping AS m ON (m.item_id += i.item_id) GROUP BY i.item_id HAVING EVERY(m.container_id != 1) OR EVERY(m.contai +ner_id != 1) IS NULL

I'm not sure of a couple things: whether this is better performance-wise than what you posted or whether EVERY (or equivalent BOOL_AND) is portable beyond PostgreSQL. However, it does translate easily into DBIx::Class:

my $rs = $schema->resultset('items'); my $items = $rs->search(undef, { join => 'mapping', group_by => [ map { "me.$_" } $rs->result_source->columns ], having => "EVERY(m.container_id != 1) OR EVERY(m.container_id != 1) +IS NULL", });
The map feels a little hackish, and I don't get to use a placeholder for the container_id, but overall, I'm not too unhappy with it.

Update: Added the additional "IS NULL" clause, necessary for items which are not contained in any container.


In reply to Re^2: Combined SQL Queries (e.g., EXCEPT) and DBIx::Class by jgallagher
in thread Combined SQL Queries (e.g., EXCEPT) and DBIx::Class by jgallagher

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.