On Tue, Jul 30, 2019, at 1:46 PM, Massimiliano della Rovere wrote:
> Years later...
> please can you give me some hints on how to write the Alias subclass and the
> function to decorate with @complile?
> I'm not expert with SQLAlchemy internals.
no idea, can you please type out the SQL you wish to generate and the table
metadata you are starting with, thanks.
>
>
> Il giorno martedì 4 marzo 2014 17:14:56 UTC+1, Michael Bayer ha scritto:
>> if you want to select columns from a function you select from it:
>>
>> from sqlalchemy import func, select, create_engine, literal_column
>> s = select([literal_column("*")]).select_from(func.generate_series(4, 5, 6))
>>
>> the thing with PG’s non-standard SQL syntax in order to give it an alias, we
>> have to hack a bit, we can use quoted_name() to create names that absolutely
>> will never be quoted, even as the name of an alias:
>>
>> from sqlalchemy import func, select, create_engine
>> from sqlalchemy.sql.elements import quoted_name
>>
>> s = select([quoted_name("a.s", False)]).select_from(func.generate_series(4,
>> 5, 6).alias(quoted_name("a(s)", False)))
>>
>> e = create_engine("postgresql://scott@localhost/test", echo=True)
>> print e.execute(s).fetchall()
>>
>> that’s probably all you need. if you need more, like in-Python SQL
>> arithmetic, (e.g. the select object doesn’t have a “s.c.s” attribute), you
>> can use a proper column to get you more of that:
>>
>> s =
>> select([literal_column("a.s").label('s')]).select_from(func.generate_series(4,
>> 5, 6).alias(quoted_name("a(s)", False)))
>>
>> still further would be creating a subclass of Alias that publishes the given
>> column names fully and uses @compiles in order to render. I might have given
>> someone that recipe at some point.
>>
>>
>>
>>
>> On Mar 4, 2014, at 1:39 AM, gbr <[email protected]> wrote:
>>
>>> I know this is an old thread, but there isn't much on the web around
>>> generate_series and SQLA, so I thought I might revive it.
>>>
>>> One of the suggestion was to use:
>>> s = func.generate_series(4,5,6).alias(cols=['a'])
>>> select([func.current_date() + s.c.a])
>>> Unfortunately, alias doesn't take a `cols` argument. What's the correct
>>> syntax with a more contemporary version of SQLA (>= 0.9)?
>>>
>>> On Sunday, April 27, 2008 2:40:35 AM UTC+10, Michael Bayer wrote:
>>>> OK r4566 of trunk also allows text() within select_from(), so you can
>>>> use textual bind params (denoted by a colon ':'):
>>>> generate_series = text("generate_series(:x, :y, :z) as s(a)")
>>>> s = select([(func.current_date() +
>>>> literal_column("s.a")).label("dates")]).select_from(generate_series)
>>>> # load up some parameters:
>>>> s = s.params(x=5, y=6, z=7)
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>> --
>>> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/sqlalchemy.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
> --
> 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/182b4066-9f7c-4dfa-ae0d-f001af4e686f%40googlegroups.com
>
> <https://groups.google.com/d/msgid/sqlalchemy/182b4066-9f7c-4dfa-ae0d-f001af4e686f%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/6540d222-f492-41ca-bd56-dbd2f7bd17ad%40www.fastmail.com.