On Wed, Feb 17, 2016 at 5:29 PM, Horcle <g...@umn.edu> wrote:

> I have the following query  db.session.query(label('sid',
> distinct(Clinical.patient_sid))) to which I would like to pad a few extra
> columns with constant values, like in the following SQL example
>
> select distinct(Clinical.patient_sid) as sid, 'stuph' as attribute
>
> from Clinical
>
>
> I tried the naive approach of db.session.query(label('sid',
> distinct(Clinical.patient_sid)),label('attribute', 'stuph')), but it
> choked.
>
> I realize this is probably very simple, but alas.
>
>
I think you can use literal_column for this, something like:

    import sqlalchemy as sa
    print sa.select([sa.literal_column("'stuph'").label('attribute')])

output:

    SELECT 'stuph' AS attribute

Beware that literal_column doesn't use bind parameters or do any escaping,
so don't use it for untrusted input.

Simon

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to