title: Add alias to subquery in SQLAlchemy
body: <p>This has been driving me nuts for the past 2 hours:</p>
  
  <p>I'm trying to query for overlapping polygons. This works nicely with PostGIS-functions, but i'm having a hard time in writing this query with SQLAlchemy.</p>
  
  <p>The proper SQL-Query is: </p>
  
  <pre><code>SELECT id FROM track WHERE ST_Overlaps(
    (
      SELECT ST_GeomFromText(
        'POLYGON((
          16.15 47.25, 
          16.15 46.57, 
          13.85 46.57, 
          13.85 47.25, 
          16.15 47.25
        ))', 
      4326)
    ), 
    track.extent
  );
  </code></pre>
  
  <p>(<strong><em>track.extent</em></strong> is a column that holds polygons in geo-format, <strong><em>ST_GeomFromText()</em></strong> creates the polygon i'm comparing them to)</p>
  
  <p>I'm using ORM and my python-code looks like this so far:</p>
  
  <pre><code>viewport = select([func.ST_GeomFromText(('POLYGON(( \
    16.15 47.25, \
    16.15 46.57, \
    13.85 46.57, \
    13.85 47.25, \
    16.15 47.25))' \
    ,4326))])
  
  DBSession.query(Track.id).filter( \
    func.ST_Overlaps(viewport, Track.extent)).all()
  </code></pre>
  
  <p>The code above is interpreted by SQLAlchemy as the following SQL-Query:</p>
  
  <pre><code>SELECT track.id AS track_id FROM track, 
  (SELECT ST_GeomFromText('
    POLYGON((
      16.15 47.25, 
      16.15 46.57, 
      13.85 46.57, 
      13.85 47.25, 
      16.15 47.25
    ))', 
    4326) AS "ST_GeomFromText_1"
  ) 
  WHERE ST_Overlaps(
    (
      SELECT ST_GeomFromText(
        'POLYGON((
          16.15 47.25, 
          16.15 46.57, 
          13.85 46.57, 
          13.85 47.25, 
          16.15 47.25
        ))', 
        4326) AS "ST_GeomFromText_1"
      ), 
    track.extent
  );
  </code></pre>
  
  <p>which yields in an ERROR: </p>
  
  <pre><code> ProgrammingError: (ProgrammingError) subquery in FROM must have an alias
   LINE 2: FROM track, (SELECT ST_GeomFromText('POLYGON((16.15759034023...
                       ^
   HINT:  For example, FROM (SELECT ...) [AS] foo.
  </code></pre>
  
  <p>How can i tell sqlalchemy to either get rid of the unnecessary <strong><em>viewport</em></strong>-subquery in the FROM-clause or add an alias to the <strong><em>viewport</em></strong>-query?</p>