Getting generic results from an ORM query?

28 views
Skip to first unread message

John Anderson

unread,
Jan 2, 2013, 8:33:37 AM1/2/13
to sqlal...@googlegroups.com
I would like to generate this query:

        SELECT a.pk, aa.response
        FROM attendee as "a"
            JOIN field as "f" on f.conference_pk = a.conference_pk
            LEFT JOIN attendee_answer as "aa" ON aa.field_pk = f.pk
                AND aa.attendee_pk = a.pk;

Using the ORM, I came up with:

        onclause = and_(
            AttendeeAnswer.field_pk==Field.pk, AttendeeAnswer.attendee_pk == Attendee.pk
        )

        query = self.session.query(Attendee).join(
                Field, Field.conference_pk == Attendee.conference_pk
        ).outerjoin(
            (AttendeeAnswer, onclause)
        ).add_entity(
            AttendeeAnswer
        ).filter(
            Attendee.conference_pk == conference_pk
        )

        results = query.all()

But this returns separate ORM objects, but in this specific case I would just like the results in a list containing the data results, rather than individual ORM objects.   Is there a way to do this or should I just pass my select statement to execute?

Michael Bayer

unread,
Jan 2, 2013, 11:39:28 AM1/2/13
to sqlal...@googlegroups.com
you can specify the columns:

q = session.query(Attendee.pk, AttendeeAnswer.response)...


John Anderson

unread,
Jan 2, 2013, 11:53:24 AM1/2/13
to sqlal...@googlegroups.com
Is there a way to do Attendee.*, AttendeeAnswer.response  instead of listing all individually? 

Michael Bayer

unread,
Jan 2, 2013, 11:54:51 AM1/2/13
to sqlal...@googlegroups.com
On Jan 2, 2013, at 11:53 AM, John Anderson wrote:

you can specify the columns:

q = session.query(Attendee.pk, AttendeeAnswer.response)...



Is there a way to do Attendee.*, AttendeeAnswer.response  instead of listing all individually? 

It should work if you put the Table object in there, Attendee.__table__ if you used declarative config.

Reply all
Reply to author
Forward
0 new messages