title: In sqlalchemy, how do I get a query that matches all rows in a table such that no row in another table links to it?
body: <p>I have two tables, "event" and "event_exception". Table "event" has a (boolean) column "regular" that indicates whether the event is to occur regularly every year or not. Table "event_exception" has a column "event_id", a column "year" and a (boolean) column "occurs".</p>
  
  <p>The data is to be interpreted this way:</p>
  
  <ul>
  <li>Some events occur regularly every year, but sometimes they exceptionally don't, which is being encoded by an "event" row with "event.regular == True" and an "event_exception" row with "event_exception.occurs == False" for every year where the event exceptionally is omitted.</li>
  <li>Some events don't occur regularly, but sometimes they exceptionally do. This is being encoded by an "event" row with "event.regular == False" and an "event_exception" row with "event_exception.occurs == True" for every year where the event exceptionally occurs.</li>
  </ul>
  
  <p>How can I write a query that matches all events that will occur this year?</p>
  
  <p>My guess was something like</p>
  
  <pre><code>session.query(Event, EventException).filter(Event.id==EventException.event_id)
  .filter(EventException.year==current_year).
  filter(or_(
      and_(Event.regular==1, EventException.occurs==0, having(count(EventException)==0)),
      and_(Event.regular==0, EventException==1, having(count(EventException)&gt;0)
  ))
  </code></pre>
  
  <p>, but I'm not sure if the <code>having</code> clause can be used within an <code>and_</code>.</p>