On Wednesday, August 25, 2021 at 3:39:34 PM UTC-7 Rob Paire wrote: > Hi Web2py > I hope someone can explains this puzzling behavior. The code below was > taken from the book, and it works as given, but when I remove the > "yes_or_no" condition from the field list parameters it causes > a KeyError('id') error, and I don't understand why. > > > * condition = db.person.name.startswith('B')* > * yes_or_no = condition.case('Yes', 'No')* > * rows = db(db.person.name <http://db.person.name> == > 'Bob').select(db.person.name <http://db.person.name>,yes_or_no > ).as_dict() * > * rows = db(db.person.name <http://db.person.name> == > 'Bob').select(db.person.ALL).as_dict() * > * ## the two select examples above works as expected* > > * rows = db(db.person.name <http://db.person.name> == > 'Bob').select(db.person.name <http://db.person.name>).as_dict() * > This last example in red throws a KeyError('id') error when trying to name > specific fields, but works fine when returning ALL fields, or when a > condition is specified. >
I don't have a complete answer, but it's the as_dict() that throws the error, because it's looking for a key that isn't there. If you do for row in rows: print row you see that row doesn't have "id" as a key; it only has 'name' as a key. as_dict() and as_list() must have a requirement for an id field, and if you make your select rows = db(db.person.name == 'Bob').select(db.person.name ,db.person,id).as_dict() it should work as expected. [The requirement isn't mentioned in the book] /dps -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/d30845f7-41c9-4f34-b2e0-789cc6112d0en%40googlegroups.com.