Cannot use add_entity to add result from subquery?

222 views
Skip to first unread message

The Devil's Programmer

unread,
Jul 14, 2009, 4:56:25 PM7/14/09
to sqlalchemy
I can do this -

comment_count_subquery = meta.Session.query(Comment.article_id,
func.count('*').label('article_comment_count')).group_by
(Comment.article_id).subquery()
query = meta.Session.query(Article,
comment_count_subquery.c.article_comment_count)

but when I do it like this -

query = meta.Session.query(Article)
comment_count_subquery = meta.Session.query(Comment.article_id,
func.count('*').label('article_comment_count')).group_by
(Comment.article_id).subquery()
query.add_entity(comment_count_subquery.c.article_comment_count)

I get the error

AttributeError: 'NoneType' object has no attribute 'with_polymorphic'

Why won't it work this way? And what is the correct way to do this? I
wish to use add_entity because sometimes the comment count does not
need to be included.

Michael Bayer

unread,
Jul 14, 2009, 5:05:48 PM7/14/09
to sqlal...@googlegroups.com
The Devil's Programmer wrote:
>
> I can do this -
>
> comment_count_subquery = meta.Session.query(Comment.article_id,
> func.count('*').label('article_comment_count')).group_by
> (Comment.article_id).subquery()
> query = meta.Session.query(Article,
> comment_count_subquery.c.article_comment_count)
>
> but when I do it like this -
>
> query = meta.Session.query(Article)
> comment_count_subquery = meta.Session.query(Comment.article_id,
> func.count('*').label('article_comment_count')).group_by
> (Comment.article_id).subquery()
> query.add_entity(comment_count_subquery.c.article_comment_count)

you'd want "add_column()" to add a column after the fact to a Query.
add_entity() and add_column() are both a little old school.

The Devil's Programmer

unread,
Jul 14, 2009, 5:55:32 PM7/14/09
to sqlalchemy
using add_column does not seem to work the same as including both
items in the query [like below]

query = meta.Session.query(Article,
comment_count_subquery.c.article_comment_count)

it does not return an iterable result

is there any way to add subqueries to the query after the query has
been created, that replicates the functionality you would expect if
you had included both items in the initial query() call?

Michael Bayer

unread,
Jul 14, 2009, 6:17:08 PM7/14/09
to sqlal...@googlegroups.com
The Devil's Programmer wrote:
>
> using add_column does not seem to work the same as including both
> items in the query [like below]
>
> query = meta.Session.query(Article,
> comment_count_subquery.c.article_comment_count)
>
> it does not return an iterable result
>
> is there any way to add subqueries to the query after the query has
> been created, that replicates the functionality you would expect if
> you had included both items in the initial query() call?

add_column(). If you can be more specific what "does not seem to work the
same", that would help.

The Devil's Programmer

unread,
Jul 14, 2009, 6:22:19 PM7/14/09
to sqlalchemy
well instead of returning an iterable array of Articles, its just
returning a single Article

The Devil's Programmer

unread,
Jul 14, 2009, 6:41:16 PM7/14/09
to sqlalchemy
here is my code

http://pastebin.com/m13075cc9

if that helps

On Jul 15, 10:22 am, "The Devil's Programmer"

The Devil's Programmer

unread,
Jul 15, 2009, 4:29:00 AM7/15/09
to sqlalchemy
ok so the problem was that I was just calling query.add_column()
without grabbing the result, ie.. query = query....

*facepalm* rookie mistake



On Jul 15, 10:41 am, "The Devil's Programmer"
Reply all
Reply to author
Forward
0 new messages