SQLFORM errors with custom id fields

45 views
Skip to first unread message

Alan Etkin

unread,
Jan 21, 2012, 5:00:03 PM1/21/12
to web2py-users
I recently updated my web2py source code from the mercurial repository

I think that something has changed in SQLFORM that is incompatible
with custom id field names declared with:

Field("other_id", ...)

On form processing, record updates stop the app with this error:

Traceback (most recent call last):
File "/home/alan/gestionlibre/gestionlibre_gui-hg/controllers/
appadmin.py", line 178, in update
if session.form.accepts(evt.args, formname=None, keepvalues=False,
dbio=False):
File "/home/alan/web2py-hg/gluon/sqlhtml.py", line 1050, in accepts
(formname_id, record_id) = (self.record[id_fieldname],
File "/home/alan/web2py-hg/gluon/dal.py", line 4987, in __getitem__
return dict.__getitem__(self, key)

I managed to solve the problem for updates by changing sqlhtml.py in
this way:

(Line 1039)
# retrieve the actual id name (for legacy databases)
try:
id_fieldname = self.table.id.name
except (IndexError, AttributeError):
# could not retrieve the table id value
id_fieldname = "id"
# former notation was self.record.id Check for
compatibility
(formname_id, record_id) = (self.record[id_fieldname],
request_vars.get('id',
None))

The problem seems to be related to SQLFORM trying to get the default
id key from table records, and may reproduce in other instances, since
I see that the record.id property is being called in other sections of
the module.

I'd continue with the fix to provide a complete modification of the
module if the solution provided has no backward compatibility or other
problems.

Thanks

Alan Etkin

unread,
Jan 21, 2012, 5:43:12 PM1/21/12
to web2py-users
A similar error is generated on validators.py at IS_NOT_IN_DB on form
delete processing.

...
File "/home/alan/web2py-hg/gluon/validators.py", line 553, in
__call__
elif str(rows[0].id) != str(self.record_id):
File "/home/alan/web2py-hg/gluon/dal.py", line 4996, in __getattr__
return self[key]
File "/home/alan/web2py-hg/gluon/dal.py", line 4987, in __getitem__
return dict.__getitem__(self, key)
KeyError: 'id'


Here are the changes to validators.py i made to avoid the exception:

(from line 542)
...
table = self.dbset.db[tablename]
...
elif str(rows[0][table.id.name]) != str(self.record_id):
...

First the special attribute id.name is retrieved to store the actual
id field name, and then, the Row.id syntax is replaced with dict
element notation

Jim Steil

unread,
Jan 22, 2012, 8:55:11 PM1/22/12
to web...@googlegroups.com
I reported the same thing in this thread:  http://groups.google.com/group/web2py/browse_thread/thread/280f6b746e2b7201

    -Jim
 property is being called in other sections of
the module.

I'd continue with the fix to provide a complete modification of the
module if the solution provided has no backward compatibility or other
problems.

Thanks


-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2012.0.1901 / Virus Database: 2109/4757 - Release Date: 01/21/12

--
Jim Steil
VP of Information Technology
Quality Liquid Feeds, Inc.
608.935.2345 office
608.341.9896 cell
Reply all
Reply to author
Forward
0 new messages