return an alias of this Join.
The default behavior here is to first produce a SELECT
construct from this Join, then to produce an
Alias from that. So given a join of the form:
j = table_a.join(table_b, table_a.c.id == table_b.c.a_id)
The JOIN by itself would look like:
table_a JOIN table_b ON table_a.id = table_b.a_id
Whereas the alias of the above, j.alias(), would in a
SELECT context look like:
(SELECT table_a.id AS table_a_id, table_b.id AS table_b_id,
table_b.a_id AS table_b_a_id
FROM table_a
JOIN table_b ON table_a.id = table_b.a_id) AS anon_1
The equivalent long-hand form, given a Join object
j, is:
from sqlalchemy import select, alias
j = alias(
select([j.left, j.right]).\
select_from(j).\
with_labels(True).\
correlate(False),
name=name
)
The selectable produced by Join.alias() features the same
columns as that of the two individual selectables presented under
a single name - the individual columns are “auto-labeled”, meaning
the .c. collection of the resulting Alias represents
the names of the individual columns using a
<tablename>_<columname> scheme:
j.c.table_a_id
j.c.table_b_a_id
Join.alias() also features an alternate
option for aliasing joins which produces no enclosing SELECT and
does not normally apply labels to the column names. The
flat=True option will call FromClause.alias()
against the left and right sides individually.
Using this option, no new SELECT is produced;
we instead, from a construct as below:
j = table_a.join(table_b, table_a.c.id == table_b.c.a_id)
j = j.alias(flat=True)
we get a result like this:
table_a AS table_a_1 JOIN table_b AS table_b_1 ON
table_a_1.id = table_b_1.a_id
The flat=True argument is also propagated to the contained
selectables, so that a composite join such as:
j = table_a.join(
table_b.join(table_c,
table_b.c.id == table_c.c.b_id),
table_b.c.a_id == table_a.c.id
).alias(flat=True)
Will produce an expression like:
table_a AS table_a_1 JOIN (
table_b AS table_b_1 JOIN table_c AS table_c_1
ON table_b_1.id = table_c_1.b_id
) ON table_a_1.id = table_b_1.a_id
The standalone alias() function as well as the
base FromClause.alias() method also support the flat=True
argument as a no-op, so that the argument can be passed to the
alias() method of any selectable.
New in version 0.9.0: Added the flat=True option to create
“aliases” of joins without enclosing inside of a SELECT
subquery.
- Parameters
name – name given to the alias.
flat –
if True, produce an alias of the left and right
sides of this Join and return the join of those
two selectables. This produces join expression that does not
include an enclosing SELECT.