MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / render_derived

Method render_derived

lib/sqlalchemy/sql/selectable.py:1961–2024  ·  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

1959 return tva
1960
1961 def render_derived(
1962 self,
1963 name: Optional[str] = None,
1964 with_types: bool = False,
1965 ) -> TableValuedAlias:
1966 """Apply "render derived" to this :class:`_sql.TableValuedAlias`.
1967
1968 This has the effect of the individual column names listed out
1969 after the alias name in the "AS" sequence, e.g.:
1970
1971 .. sourcecode:: pycon+sql
1972
1973 >>> print(
1974 ... select(
1975 ... func.unnest(array(["one", "two", "three"]))
1976 ... .table_valued("x", with_ordinality="o")
1977 ... .render_derived()
1978 ... )
1979 ... )
1980 {printsql}SELECT anon_1.x, anon_1.o
1981 FROM unnest(ARRAY[%(param_1)s, %(param_2)s, %(param_3)s]) WITH ORDINALITY AS anon_1(x, o)
1982
1983 The ``with_types`` keyword will render column types inline within
1984 the alias expression (this syntax currently applies to the
1985 PostgreSQL database):
1986
1987 .. sourcecode:: pycon+sql
1988
1989 >>> print(
1990 ... select(
1991 ... func.json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]')
1992 ... .table_valued(column("a", Integer), column("b", String))
1993 ... .render_derived(with_types=True)
1994 ... )
1995 ... )
1996 {printsql}SELECT anon_1.a, anon_1.b FROM json_to_recordset(:json_to_recordset_1)
1997 AS anon_1(a INTEGER, b VARCHAR)
1998
1999 :param name: optional string name that will be applied to the alias
2000 generated. If left as None, a unique anonymizing name will be used.
2001
2002 :param with_types: if True, the derived columns will include the
2003 datatype specification with each column. This is a special syntax
2004 currently known to be required by PostgreSQL for some SQL functions.
2005
2006 """ # noqa: E501
2007
2008 # note: don't use the @_generative system here, keep a reference
2009 # to the original object. otherwise you can have reuse of the
2010 # python id() of the original which can cause name conflicts if
2011 # a new anon-name grabs the same identifier as the local anon-name
2012 # (just saw it happen on CI)
2013
2014 # construct against original to prevent memory growth
2015 # for repeated generations
2016 new_alias: TableValuedAlias = TableValuedAlias._construct(
2017 self.element,
2018 name=name,

Calls 1

_constructMethod · 0.45