title: Postgresql: Subquery in FROM must have an alias - with multiple joins
body: <p>I get the following error:</p>
  
  <pre><code>ERROR:  subquery in FROM must have an alias
  LINE 11:   (SELECT "domiciles"."id" AS id,
             ^
  HINT:  For example, FROM (SELECT ...) [AS] foo.
  </code></pre>
  
  <p>with the following SQL query:</p>
  
  <pre><code>SELECT "domiciles".*
  FROM "domiciles"
  LEFT OUTER JOIN
    (SELECT "domiciles"."id" AS id,
            string_agg("locations"."name"::text, ' ') AS name
     FROM "domiciles"
     INNER JOIN "locations" ON "locations"."id" = "domiciles"."place_id"
     AND "locations"."type" IN ('Place')
     GROUP BY "domiciles"."id") place ON place.id = "domiciles"."id"
  LEFT OUTER JOIN
    (SELECT "domiciles"."id" AS id,
            string_agg("accounts"."email"::text, ' ') AS email
     FROM "domiciles"
     INNER JOIN "accounts" ON "accounts"."id" = "domiciles"."user_id"
     AND "accounts"."type" IN ('User')
     GROUP BY "domiciles"."id") user ON user.id = "domiciles"."id"
  WHERE "domiciles"."deleted_at" IS NULL
  </code></pre>
  
  <p><strong>I have tried to add AS 'some_text' in many parts of the query, but I cannot solve this problem. Any idea?</strong></p>
  
  <p>Here the complete SQL query to have an idea what I want to do:</p>
  
  <pre><code>SELECT "domiciles".*,
         ((ts_rank((to_tsvector('german', unaccent(coalesce("domiciles"."is_default"::text, ''))) || to_tsvector('german', unaccent(coalesce("domiciles"."created_at"::text, ''))) || to_tsvector('german', unaccent(coalesce("domiciles"."updated_at"::text, ''))) || to_tsvector('german', unaccent(coalesce(place.name::text, ''))) || to_tsvector('german', unaccent(coalesce(user.email::text, ''))) || to_tsvector('german', unaccent(coalesce(owner.email::text, '')))), (to_tsquery('german', ''' ' || unaccent('abc') || ' ''' || ':*')), 0))) AS pg_search_rank
  FROM "domiciles"
  LEFT OUTER JOIN
    (SELECT "domiciles"."id" AS id,
            string_agg("locations"."name"::text, ' ') AS name
     FROM "domiciles"
     INNER JOIN "locations" ON "locations"."id" = "domiciles"."place_id"
     AND "locations"."type" IN ('Place')
     AND "locations"."deleted_at" IS NULL
     GROUP BY "domiciles"."id") place ON place.id = "domiciles"."id"
  LEFT OUTER JOIN
    (SELECT "domiciles"."id" AS id,
            string_agg("accounts"."email"::text, ' ') AS email
     FROM "domiciles"
     INNER JOIN "accounts" ON "accounts"."id" = "domiciles"."user_id"
     AND "accounts"."type" IN ('User')
     AND "accounts"."deleted_at" IS NULL
     GROUP BY "domiciles"."id") user ON user.id = "domiciles"."id"
  LEFT OUTER JOIN
    (SELECT "domiciles"."id" AS id,
            string_agg("accounts"."email"::text, ' ') AS email
     FROM "domiciles"
     INNER JOIN "locations" ON "locations"."id" = "domiciles"."place_id"
     AND "locations"."type" IN ('Place')
     AND "locations"."deleted_at" IS NULL
     INNER JOIN "accounts" ON "accounts"."id" = "locations"."user_id"
     AND "accounts"."type" IN ('User')
     AND "accounts"."deleted_at" IS NULL
     GROUP BY "domiciles"."id") owner ON owner.id = "domiciles"."id"
  WHERE "domiciles"."deleted_at" IS NULL
    AND "domiciles"."user_id" = $1
    AND (((to_tsvector('german', unaccent(coalesce("domiciles"."is_default"::text, ''))) || to_tsvector('german', unaccent(coalesce("domiciles"."created_at"::text, ''))) || to_tsvector('german', unaccent(coalesce("domiciles"."updated_at"::text, ''))) || to_tsvector('german', unaccent(coalesce(place.name::text, ''))) || to_tsvector('german', unaccent(coalesce(user.email::text, ''))) || to_tsvector('german', unaccent(coalesce(owner.email::text, '')))) @@ (to_tsquery('german', ''' ' || unaccent('abc') || ' ''' || ':*'))))
  </code></pre>