On Wed, Apr 27, 2011 at 8:42 PM, Michael Bayer <[email protected]>wrote:
>
> 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.
>
>
Hi,
When i tested with 0.7b4 version of SQLAlchemy the above issue is
resolved.Earlier i was testing with 0.6.6 version of SQLAlchemy.
After doing the union_all when i try to do the
query.order_by(desc('crt_dt')) on query object i am getting the programming
error Invalid column name 'crt_dt'.since it is aliased, it is not getting
the correct column name.
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
if bundle:
----------------------------> if more than 1 table.
query = query.order_by(desc('crt_dt'))
return result_query.all()
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()
Error when i try to do query.order_by
--------------------------------------------
*** ProgrammingError: (ProgrammingError) ('42S22', 207,
"[OpenLink][ODBC][SQL Server]Invalid column name 'crt_dt'. (207)", 'SELECT
TOP 10 anon_1.anon_2_row_num AS anon_1_anon_2_row_num,
anon_1.anon_2_test_msg AS anon_1_anon_2_test_msg, anon_1.anon_2_crt_dt AS
anon_1_anon_2_crt_dt, anon_1.anon_2_name_file AS anon_1_anon_2_name_file
\nFROM (SELECT anon_2.row_num AS anon_2_row_num, anon_2.test_msg AS
anon_2_test_msg, anon_2.crt_dt AS anon_2_crt_dt, anon_2.name_file AS
anon_2_name_file \nFROM (SELECT table1.row_num AS row_num, table1.test_msg
AS test_msg, table1.crt_dt AS crt_dt, table4.name_file AS name_file \nFROM
dbo.log_client AS table1 JOIN table4 ON table4.table4_idn =
table1.table4_idn \nWHERE table4.table4_idn = ? AND table1.crt_dt BETWEEN ?
AND ? UNION ALL SELECT table2.row_num AS row_num, table2.test_msg AS
test_msg, table2.crt_dt AS crt_dt, table4.name_file AS name_file \nFROM
dbo.log_employer AS table2 JOIN table4 ON table4.table4_idn =
table2.table4_idn \nWHERE table4.table4_idn = ? AND table2.crt_dt BETWEEN ?
AND ?) AS anon_2 UNION ALL SELECT table3.row_num AS row_num, table3.test_msg
AS test_msg, table3.crt_dt AS crt_dt, table4.name_file AS name_file \nFROM
dbo.log_group AS table3 JOIN table4 ON table4.table4_idn = table3.table4_idn
\nWHERE table4.table4_idn = ? AND table3.crt_dt BETWEEN ? AND ?) AS anon_1
ORDER BY crt_dt DESC' ('218', datetime.datetime(2011, 1, 1, 0, 0),
datetime.datetime(2011, 4, 29, 0, 0), '218', datetime.datetime(2011, 1, 1,
0, 0), datetime.datetime(2011, 4, 29, 0, 0), '218', datetime.datetime(2011,
1, 1, 0, 0), datetime.datetime(2011, 4, 29, 0, 0))
--
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.