I added query = None after line 668 which is the line containing for key in keys:
just before the line if not key in sql_fields_old: and got rid of all the query = None statements in the if elif branches plus got rid of the else branch. I am happy to report this allowed the application to run completely without error. I don't use all the features of web2py but then I am sure very few of us do. Ron