On Thu, Oct 15, 2020, at 2:52 AM, Kotofos online wrote:
> 
> Hi,
> Could you shed some light on what I might be doing incorrectly? I have this 
> text() SELECT * query on top of a one-column sub-query and in the result, I 
> am not getting that column name.
> 
> ```
> stmt = text('select "FirstName", from "Customer"')
> stmt = select('*').select_from(stmt.columns().alias())
> print(stmt)
> 
> SELECT *
> FROM (select "FirstName",  from "Customer") AS anon_1
> ```
> 
> This is works, but produces incorrect column name in the output:
> ```
> res = engine.execute(stmt)
> keys = res.fetchall()[0].keys()
> print(keys)

When using select(), the names in the result rows come from those which were 
provided to the select() method.  they are matched to those of the actual 
cursor if possible but not if they don't match.


> 
> ['*']
> ```
> 
> But when the subquery has two columns, then it works as expected:
> ```
> stmt = text('select "FirstName", "LastName" from "Customer"')
> stmt = select('*').select_from(stmt.columns().alias())
> res = engine.execute(stmt)
> keys = res.fetchall()[0].keys()
> print(keys)
> 
> ['FirstName', 'LastName']
> ```
> So, is there a better way to wrap text query? Why column name is lost in the 
> first case? Is it a bug?

it's basically undefined behavior you're dealing with.  you wouldn't want to 
pass a "*" to select().   I'm sure there's more context to what you actually 
need to do, but in the example above, there's no need to use a subquery, just 
invoke the text() construct directly.


> 
> Tested on 1.3.20 and 1.2.19 
> 

> --
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/c0be052d-1c20-49d1-a73d-875b4a7afef9n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/c0be052d-1c20-49d1-a73d-875b4a7afef9n%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/e7967445-b80a-4696-8d7e-db36899f0a10%40www.fastmail.com.

Reply via email to