>
> Hi all,
> Is there a way to use generate_series with SQLAlchemy?
>
> http://www.postgresql.org/docs/8.2/interactive/functions-srf.html
>
> generate_series = select([column('i')],
> from_obj=[func.generate_series(bindparam('start'),
> bindparam('end'))])
>
> zrows = select([generate_series.params(start=1,
> end=20).c.i]).label('series')
>
> is the best stab I've made at it, but I don't actually understand the
> syntax in the PGSQL docs:
>
> select current_date + s.a as dates from generate_series(0,14,7) as
> s(a);
>
that particular syntax is an aliasing syntax that we plan on
supporting in the near future, so the above would look possibly like
s = func.generate_series(4,5,6).alias(cols=['a'])
select([func.current_date() + s.c.a])
you can hardwire this stuff using text right now:
select([(func.current_date() +
literal_column
("s.a")).label("dates")]).select_from("generate_series(0, 14, 7) as
s(a)")
I just noticed that the text() construct, which would allow the
bindparams to happen, is not being accepted into select_from() so i've
added ticket #1014 for that.
> As a side question, how would I add static columns to a select
> statement? e.g. based on the pseudocode above:
> zrows = select([generate_series.params(start=1, end=20).c.i], 0, 0,
> 0).label('series')
> to add 3 columns of 0 value to each row generated.
you should be able to put plain strings in the columns clause:
select(["foo", "bar", "bat"])
this is shorthand for using the "literal_column()" construct which you
can use anywhere in a column expression to produce textual SQL
expressions.
>
> Hm, yes, so is:
>
> Traceback (most recent call last):
> File "/Users/LukeIannini/Checkout/trunk/adpinion_web/
> HistoryAlchemy.py", line 46, in <module>
> allstats = union_all(stats, zeros)
> File "/Library/Python/2.5/site-packages/SQLAlchemy-0.4.1-py2.5.egg/
> sqlalchemy/sql/expression.py", line 498, in union_all
> return _compound_select('UNION ALL', *selects, **kwargs)
> File "/Library/Python/2.5/site-packages/SQLAlchemy-0.4.1-py2.5.egg/
> sqlalchemy/sql/expression.py", line 780, in _compound_select
> return CompoundSelect(keyword, *selects, **kwargs)
> File "/Library/Python/2.5/site-packages/SQLAlchemy-0.4.1-py2.5.egg/
> sqlalchemy/sql/expression.py", line 2895, in __init__
> self.oid_column = self._proxy_column(s.oid_column)
> File "/Library/Python/2.5/site-packages/SQLAlchemy-0.4.1-py2.5.egg/
> sqlalchemy/sql/expression.py", line 3323, in oid_column
> oid = f.oid_column
> AttributeError: '_TextFromClause' object has no attribute 'oid_column'
>
> when trying to union_all the result of a (on its own, seemingly
> working) generate_series select a manifestation of the bug you
> mentioned or am I doing something else wrong?
no, thats an entirely different bug :) thats been fixed as of
recent releases so upgrade to 0.4.5.
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)
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)?
--
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 tosqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
To unsubscribe from this group and stop receiving emails from it, send an email tosqlal...@googlegroups.com.
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.
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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 sqlalchemy+...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/182b4066-9f7c-4dfa-ae0d-f001af4e686f%40googlegroups.com.