rails/rails — Issue #4444: Table Alias naming is working for joins, but not for subqueries.
Author: daniel2d2art
Created: 2012-01-13 04:18:39+00:00
State: open

Table Alias naming is working for joins, but not for subqueries.

for example this query that aliasing "t1" and "t2" for "foo" table:
SELECT t1.* FROM foo AS t1 WHERE ( EXISTS (SELECT t2.ent_id, t2.type FROM foo AS t2 GROUP BY t2.ent_id, t2.type HAVING t1.ent_id = t2.ent_id))

If use AR::QueryMethods::From to specify the from clause like .from('foo AS t1'), the query is bad formated, you can set handly the where fields, but selects fiels are preceded by "foo" table name, no by the table aliases. And can't use .select() because it makes results as ReadOnly.

This makes imposible make ActiveRecord::Relation query like this:

select *
from cars a
where
exists ( select a2.model_id, a2.group_id
from cars a2
where a2.manufactured_by = ?
group by a2.model_id, a2.group_id
having a.model_id=a2.model_id
and a.group_id=a2.group_id
and a.released_at=max(a2.released_at)
)
order by a.released_at desc, a.model_id;