MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / subquery

Method subquery

lib/sqlalchemy/orm/query.py:629–672  ·  view source on GitHub ↗

Return the full SELECT statement represented by this :class:`_query.Query`, embedded within an :class:`_expression.Alias`. Eager JOIN generation within the query is disabled. .. seealso:: :meth:`_sql.Select.subquery` - v2 comparable method. :pa

(
        self,
        name: Optional[str] = None,
        with_labels: bool = False,
        reduce_columns: bool = False,
    )

Source from the content-addressed store, hash-verified

627 return stmt
628
629 def subquery(
630 self,
631 name: Optional[str] = None,
632 with_labels: bool = False,
633 reduce_columns: bool = False,
634 ) -> Subquery:
635 """Return the full SELECT statement represented by
636 this :class:`_query.Query`, embedded within an
637 :class:`_expression.Alias`.
638
639 Eager JOIN generation within the query is disabled.
640
641 .. seealso::
642
643 :meth:`_sql.Select.subquery` - v2 comparable method.
644
645 :param name: string name to be assigned as the alias;
646 this is passed through to :meth:`_expression.FromClause.alias`.
647 If ``None``, a name will be deterministically generated
648 at compile time.
649
650 :param with_labels: if True, :meth:`.with_labels` will be called
651 on the :class:`_query.Query` first to apply table-qualified labels
652 to all columns.
653
654 :param reduce_columns: if True,
655 :meth:`_expression.Select.reduce_columns` will
656 be called on the resulting :func:`_expression.select` construct,
657 to remove same-named columns where one also refers to the other
658 via foreign key or WHERE clause equivalence.
659
660 """
661 q = self.enable_eagerloads(False)
662 if with_labels:
663 q = q.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
664
665 stmt = q._get_select_statement_only()
666
667 if TYPE_CHECKING:
668 assert isinstance(stmt, Select)
669
670 if reduce_columns:
671 stmt = stmt.reduce_columns()
672 return stmt.subquery(name=name)
673
674 def cte(
675 self,

Callers 15

_table_options_queryMethod · 0.45
_constraint_queryMethod · 0.45
_index_queryMethod · 0.45
_enum_queryMethod · 0.45
_domain_queryMethod · 0.45
_has_table_queryMethod · 0.45
get_table_namesMethod · 0.45
_comment_queryMethod · 0.45
_legacy_from_selfMethod · 0.45
_set_opMethod · 0.45

Calls 4

enable_eagerloadsMethod · 0.95
reduce_columnsMethod · 0.80
set_label_styleMethod · 0.45

Tested by 15

test_subqueryMethod · 0.36
test_aliases_and_ssMethod · 0.36
test_subquery_proxyMethod · 0.36
test_with_colsMethod · 0.36
test_adaptationMethod · 0.36
test_plain_selectMethod · 0.36
test_unionMethod · 0.36