Select duplicates itself when using column() on its own column

Issue #3987 resolved
Zac Goldstein created an issue

I suspect Select.column() is intended to be passed columns from a table rather than itself, but if that's the case a note in the documentation would helpful. My actual usage was a lot more complicated than the example and this took a while to figure out.

from sqlalchemy import select, table, column, func

t1 = table("t1", column("c1"))
s1 = select([t1])

s2 = s1.column(func.min(s1.c.c1))

#  >>> str(s2)
#  'SELECT t1.c1, min(c1) AS min_1 \nFROM t1, (SELECT t1.c1 AS c1 \nFROM t1)'

s3 = s1.column(func.min(t1.c.c1))

#  Note that using the column from the table rather than the Select works fine
#  >>> str(s3)
#  'SELECT t1.c1, min(t1.c1) AS min_1 \nFROM t1'

Comments (4)

  1. Mike Bayer repo owner

    Add links to with_only_columns to Select.column, append_column

    Provide a brief example for these two methods indicating that typically a table-bound (or other selectable) column is appended here, then link to with_only_columns documentation which has in-depth guidelines already including that one should not append columns from the current select to itself.

    Change-Id: I0742405a7f3c41450d337b9c633519d9cc101dfb Fixes: #3987

    → <<cset 4352e220ac04>>

  2. Mike Bayer repo owner

    Add links to with_only_columns to Select.column, append_column

    Provide a brief example for these two methods indicating that typically a table-bound (or other selectable) column is appended here, then link to with_only_columns documentation which has in-depth guidelines already including that one should not append columns from the current select to itself.

    Change-Id: I0742405a7f3c41450d337b9c633519d9cc101dfb Fixes: #3987 (cherry picked from commit 4352e220ac04d09e120c441e79b1ac12c7ca2c45)

    → <<cset aa14644d1ca0>>

  3. Mike Bayer repo owner

    Add links to with_only_columns to Select.column, append_column

    Provide a brief example for these two methods indicating that typically a table-bound (or other selectable) column is appended here, then link to with_only_columns documentation which has in-depth guidelines already including that one should not append columns from the current select to itself.

    Change-Id: I0742405a7f3c41450d337b9c633519d9cc101dfb Fixes: #3987 (cherry picked from commit 4352e220ac04d09e120c441e79b1ac12c7ca2c45)

    → <<cset 34a16747c004>>

  4. Log in to comment