Problem accessing Rows/Row

55 views
Skip to first unread message

MichaelF

unread,
Sep 13, 2012, 6:16:44 PM9/13/12
to web...@googlegroups.com
I do a similar set of steps, yet one of the sets acts differently. I'm trying to loop through a query return (Rows), but I'm obviously missing something basic.

I do a query and 'print' the return. Here's a snippet from my controller:

line 144:
instAttch = db((db.Meet.id == request.args(1)) &
               (db.Addl_institution_info.Start_date < db.Meet.Start_date) &
               (db.Addl_institution_info.End_date > db.Meet.Start_date) &
               (db.Participant_team.Is_home_team)).select(
                 db.Addl_info_item.ALL,
                 join = [db.Participant_team.on(
                           db.Meet.id == db.Participant_team.Meet),
                         db.Team.on(db.Participant_team.Team == db.Team.id),
                         db.Institution.on(db.Team.Institution ==
                            db.Institution.id),
                         db.Addl_institution_info.on(
                            db.Addl_institution_info.Institution ==
                              db.Institution.id),
                         db.Addl_info_item.on(db.Addl_info_item.id ==
                           db.Addl_institution_info.Addl_info_item)])

...
Later in the same function (displayed with line numbers):
240: print 'Start loop at 240; instAttch:\n'
241: print instAttch
242: for attch in instAttch:
243:    print 'attch:\n'
244:    #
245:    print attch
246:    if attch.Addl_info_item.Email_text:

The output from line 241 looks like a Rows object to my rookie eye:

Start loop at 240; instAttch:

Addl_info_item.id,Addl_info_item.File_path,Addl_info_item.Email_text,Addl_info_i
tem.Content_id,Addl_info_item.Internal_description
2,C:\Users\mjf\Documents\MJF\WebSite\NCAA\private\Test DU Parking pass.dat,Test
email text: addl_info_item 2,,item 2

attch:

<Row {'Internal_description': 'item 2', 'Addl_pool_info': <gluon.dal.Set object
at 0x05DAA4D0>, 'update_record': <function <lambda> at 0x05F158B0>, 'Addl_instit
ution_info': <gluon.dal.Set object at 0x05DAA6B0>, 'File_path': 'C:\\Users\\mjf\
\Documents\\MJF\\WebSite\\NCAA\\private\\Test DU Parking pass.dat', 'Email_text'
: 'Test email text: addl_info_item 2', 'Content_id': '', 'Addl_meet_info': <gluo
n.dal.Set object at 0x05DAA1F0>, 'id': 2, 'delete_record': <function <lambda> at
 0x05F15F30>}>

I do a 'for ...' on instAttch at line 242 and get:
<type 'exceptions.KeyError'> 'Addl_info_item'

The traceback tells me it's at line 246:
   if attch.Addl_info_item.Email_text:

What am I missing?

villas

unread,
Sep 13, 2012, 9:36:37 PM9/13/12
to web...@googlegroups.com
I don't really know,  but I would suggest playing around with things on the commandline.

python web2py
.py -S yourapp -N -M

>>> rows = db(db.Addl_info_item.id > 0).select()
>>> for f in rows[0]: print f
>>> for r in rows: print r.Email_text

etc etc


You can put more and more code in there and hopefully see what's going wrong.

BTW  one thing I would advise against is using mixed case for table and field names.  Obviously it is easier to make typos if you have to worry about the case.  However I have also seen issues with certain DBs.  If that is something you can address,  then make everything lowercase,  it would probably make things much easier for you in the long run.

Regards,  D

MichaelF

unread,
Sep 13, 2012, 10:43:04 PM9/13/12
to web...@googlegroups.com
The problem is that I need to change the problem line from:
246:    if attch.Addl_info_item.Email_text:

to:
246:    if attch.Email_text:

My previous 'select' statements retrieved fields from several tables, while this one (the 'problem' one) retrieves from only one table. So, if I understand correctly, if my fields come from only one table I leave off the table name when 'addressing' the row, else I do use the table name. That seems 'unorthogonal'. Am I misunderstanding?

lyn2py

unread,
Sep 14, 2012, 4:11:57 AM9/14/12
to web...@googlegroups.com
If you have data from one table only, leave out the table name. (which is in your case)
If you have data from more than one table, you need to use the table name to access the correct info. 

This has always been the way web2py functions. :)

I think it is a matter of perspective. When you only access data from one table, it doesn't make sense to need to indicate the table name (more code, and unnecessary. Some of our table names can be rather complicated acronyms too.). When you retrieve from more than one table, to specifically access a particular field from a particular table, we need the table name. It is out of necessity rather than following some kind of structured rule.

Does that make sense?

MichaelF

unread,
Sep 14, 2012, 9:12:34 AM9/14/12
to web...@googlegroups.com
Yes, it makes sense, and I've already modified my code; thanks. It's the inconsistency that gets me. It's not a big deal, but still. If I add another field from another table to my select, then I have to change all my field references to include the table references. I suspect the Rows/Row iterator/accessor is already doing something, and if so it could allow the unnecessary table references.

Anyway, not a big deal; I'm glad my code is working. Thanks again.
Reply all
Reply to author
Forward
0 new messages