query question

42 views
Skip to first unread message

lars van gemerden

unread,
Sep 4, 2013, 7:05:05 AM9/4/13
to sqlal...@googlegroups.com
I  think i must be reading over something, but:

is there a way to delay the selection of attributes in a query; something like

     >>> session.query(Person).filter(Person.age > 100).select(Person.name).first()

     >>>  (u"Ancient Bob",)

Cheers, Lars

Ofir Herzas

unread,
Sep 4, 2013, 7:21:49 AM9/4/13
to sqlal...@googlegroups.com

Haven't tried it myself, but I think you can use the add_columns method of Query.

Check out http://docs.sqlalchemy.org/ru/latest/orm/query.html

--
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 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.

Simon King

unread,
Sep 4, 2013, 7:24:28 AM9/4/13
to sqlal...@googlegroups.com
I'm not sure quite what you mean, but the with_entities method of
Query would seem to be the closest to your example:

http://docs.sqlalchemy.org/en/rel_0_8/orm/query.html#sqlalchemy.orm.query.Query.with_entities

I assume you know you can also do this:

session.query(Person.name).filter(Person.age > 100).first()

You can also defer certain columns so that they will be loaded lazily:

http://docs.sqlalchemy.org/en/rel_0_8/orm/mapper_config.html#deferred-column-loading

Hope that helps,

Simon

lars van gemerden

unread,
Sep 9, 2013, 8:43:33 AM9/9/13
to sqlal...@googlegroups.com
Ok, i've tried both (and each separately), as in:

        r1 = Report(number = 1, title = "The Bird") 
        r2 = Report(number = 2, title = "The Bucket")
        session.add_all([r1, r2])
        session.commit()
        q = self.session.query(Report.title)
        q.with_entities(Report.number)
        q.add_columns(Report.number)
        print q.all()

printing:

        [(u'The Bird',), (u'The Bucket',)]

what i would like to see is:

       [(u'The Bird', 1), (u'The Bucket', 2)]
             
or something similar.

Cheers, Lars

lars van Gemerden

unread,
Sep 9, 2013, 9:04:20 AM9/9/13
to sqlal...@googlegroups.com
OK, never mind |-)

should be:

       q = q.with_entities(Report.number)
       q = q.add_columns(Report.number)

sry



--
You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/7hA4UZV8uaA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@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.



--
====================================
Lars van Gemerden
la...@rational-it.com
+31 6 26 88 55 39
====================================
Reply all
Reply to author
Forward
0 new messages