[Sqlalchemy-tickets] Issue #3652: Clarify unnest example in FunctionElement.alias documentation (zz
Brought to you by:
zzzeek
From: Sebastian B. <iss...@bi...> - 2016-02-16 09:38:58
|
New issue 3652: Clarify unnest example in FunctionElement.alias documentation https://bitbucket.org/zzzeek/sqlalchemy/issues/3652/clarify-unnest-example-in Sebastian Bank: Using `func.unnest` to unpack a postgres array: ```python from sqlalchemy import Column, Integer, Text, select, column, func from sqlalchemy.dialects.postgresql import ARRAY from sqlalchemy.ext.declarative import declarative_base class Post(declarative_base()): __tablename__ = 'post' id = Column(Integer, primary_key=True) title = Column(Text, nullable=False) tags = Column(ARRAY(Text), nullable=False) query = select([Post.id, column('tag')])\ .select_from(func.unnest(Post.tags).alias('tag')) print(query) ``` As wanted, this gives `SELECT post.id, tag FROM post, unnest(post.tags) AS tag` (which is postgres-specific syntax, I guess). I found the example given in [the documentation of FunctionElement.alias](https://bitbucket.org/zzzeek/sqlalchemy/src/366f97b5617af0d15cfaf594ec5ef0408c70e873/lib/sqlalchemy/sql/functions.py?at=master&fileviewer=file-view-default#functions.py-198) somewhat confusing because the SELECT-clause does not actually use the unpacked data: ```python stmt = select([column('data')]).select_from( func.unnest(Table.data).alias('data_view')) ``` Wouldn't it make more sense like this (following the example above)? ```python stmt = select([column('item')]).select_from( func.unnest(Table.data).alias('item')) ``` Or maybe this is not the right use of `FunctionElement.alias`? |