On Apr 27, 2011, at 10:58 AM, monster jacker wrote:
> >>The Query should return to you tuples that have names like "row_num" and
> >>"test_msg", which are linked to the "anon" names that >>it generates:
>
> >> for row in myquery:
> >> print row.row_num, row.test_msg
>
> >>that is, the "anon_x" names do not matter. they are an artifact of how the
> >>Query does its work and the result rows are translated >>back to the
> >>constructs you gave it originally.
>
> If i have method as below
>
> def second_method(self):
> for tbl in Table: ---- for loop is used
> since we have 3 tables and doing union of them
> qry = some_method()
> query = query.union_all(qry) if query else qry
> return result_query
>
> def some_method(self):
> query = self.session.query(Table.row_num.label('row_num'),
> Table.test_msg.label('test_msg'),
> Table.crt_dt.label('crt_dt'),
> Table2.name_file.label('name_file')).join(
> (Table2, Table2.some_idn == Table.some_idn))
> return query
>
> record = second_method()
>
>
> The above method returns the query object . when we try record[0] we will
> get the result set
> [(1, None, datetime.datetime(2011, 2, 24, 12, 37, 58, 123000), 'test.txt')]
>
> when i try to get the keys record[0].__dict__.keys() it gives the result
>
> [u'%(215049772 anon)s_name_file', u'%(215049772 anon)s_crt_dt', u'%(215049772
> anon)s_row_num', '_labels', u'%(215049772 anon)s_test_msg']
>
> so as you mentioned in previous mail if i try
>
> for row in record:
> print row.row_num, row.test_msg
>
> i am getting the attribute error : *** AttributeError: 'NamedTuple'
> object has no attribute 'row_num'
also, while we're waiting for the bug, immediate workaround is:
import operator
def named_tuple(*names):
return type("MyTuple", (tuple,),
dict(
(name, property(operator.itemgetter(i)))
for i, name in enumerate(names)
)
)
my_workaround_row = named_tuple('row_num', 'test_msg', 'crt_dt', 'name_file')
return [my_workaround_row(row) for row in query]
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.