MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / render_derived

Method render_derived

lib/sqlalchemy/sql/selectable.py:2013–2076  ·  view source on GitHub ↗

Apply "render derived" to this :class:`_sql.TableValuedAlias`. This has the effect of the individual column names listed out after the alias name in the "AS" sequence, e.g.: .. sourcecode:: pycon+sql >>> print( ... select( ...

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

Source from the content-addressed store, hash-verified

2011 return tva
2012
2013 def render_derived(
2014 self,
2015 name: Optional[str] = None,
2016 with_types: bool = False,
2017 ) -> TableValuedAlias:
2018 """Apply "render derived" to this :class:`_sql.TableValuedAlias`.
2019
2020 This has the effect of the individual column names listed out
2021 after the alias name in the "AS" sequence, e.g.:
2022
2023 .. sourcecode:: pycon+sql
2024
2025 >>> print(
2026 ... select(
2027 ... func.unnest(array(["one", "two", "three"]))
2028 ... .table_valued("x", with_ordinality="o")
2029 ... .render_derived()
2030 ... )
2031 ... )
2032 {printsql}SELECT anon_1.x, anon_1.o
2033 FROM unnest(ARRAY[%(param_1)s, %(param_2)s, %(param_3)s]) WITH ORDINALITY AS anon_1(x, o)
2034
2035 The ``with_types`` keyword will render column types inline within
2036 the alias expression (this syntax currently applies to the
2037 PostgreSQL database):
2038
2039 .. sourcecode:: pycon+sql
2040
2041 >>> print(
2042 ... select(
2043 ... func.json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]')
2044 ... .table_valued(column("a", Integer), column("b", String))
2045 ... .render_derived(with_types=True)
2046 ... )
2047 ... )
2048 {printsql}SELECT anon_1.a, anon_1.b FROM json_to_recordset(:json_to_recordset_1)
2049 AS anon_1(a INTEGER, b VARCHAR)
2050
2051 :param name: optional string name that will be applied to the alias
2052 generated. If left as None, a unique anonymizing name will be used.
2053
2054 :param with_types: if True, the derived columns will include the
2055 datatype specification with each column. This is a special syntax
2056 currently known to be required by PostgreSQL for some SQL functions.
2057
2058 """ # noqa: E501
2059
2060 # note: don't use the @_generative system here, keep a reference
2061 # to the original object. otherwise you can have reuse of the
2062 # python id() of the original which can cause name conflicts if
2063 # a new anon-name grabs the same identifier as the local anon-name
2064 # (just saw it happen on CI)
2065
2066 # construct against original to prevent memory growth
2067 # for repeated generations
2068 new_alias: TableValuedAlias = TableValuedAlias._construct(
2069 self.element,
2070 name=name,

Calls 1

_constructMethod · 0.45