I’m new to Web2py and am unable to understand the error that the ticket is throwing up.
Can someone explain the error and why it is occurring?
Here is the ticket:
Ticket ID
127.0.0.1.2012-08-29.01-43-16.edfb1953-fd71-4aa6-a768-815fe46fe273
‘Expression’ object has no attribute ‘strip’
Traceback
Traceback (most recent call last):
File "/home/user/Web2py/web2py/gluon/restricted.py", line 205, in restricted exec ccode in environment
File "/home/user/Web2py/web2py/applications/SocialImage/models/db.py", line 12, in format = '%(title)s')
File "/home/user/Web2py/web2py/gluon/dal.py", line 6320, in define_table
polymodel=polymodel)
File "/home/user/Web2py/web2py/gluon/dal.py", line 598, in create_table referenced = field.type[10:].strip()
AttributeError: 'Expression' object has no attribute 'strip'
Error snapshot
('Expression' object has no attribute 'strip')
Function argument list
(self=, table=, 'id': }>, migrate=True, fake_migrate=False, polymodel=None)
Code listing
sortable += 1
k = field.name
if isinstance(field.type,SQLCustomType):
ftype = field.type.native or field.type.type
elif field.type.startswith('reference'):
referenced = field.type[10:].strip()
constraint_name = self.constraint_name(tablename, field.name)
if hasattr(table,'_primarykey'):
rtablename,rfieldname = referenced.split('.')
rtable = table._db[rtablename]
CODE:
db = DAL("sqlite://storage.sqlite")
db.define_table('user',
Field('uname',unique=True),
Field('name'),
Field('email'))
db.define_table('image',
Field('title', unique=True),
Field('file', 'upload'),
Field('uploader_name', db.user.uname),
format = '%(title)s')
db.define_table('comment',
Field('image_id', db.image),
Field('authors', db.user.uname),
Field('body', 'text'))
db.user.email.requires=IS_EMAIL()
db.user.uname.requires=IS_NOT_IN_DB(db,db.uname)
db.user.name=IS_NOT_EMPTY()
db.image.uploader.requires=IS_IN_DB(db,db.user.uname)
db.comment.image_id.requires=IS_IN_DB(db,db.image.id,'%(title)s')
db.comment.image_id.writable=db.comment.image_id.readable=False
You are defining your reference fields incorrectly — they should refer to a table, not to a specific field within that table. For example, you have:
which should be:
Note, the reference field will not store the actual uname from the user table — it will store the id of the referenced record from the user table. From that reference, you can then get the uname if you need it.
Also, the upcoming 2.0 release of web2py includes a new “lazy tables” features that defines tables lazily (full definition is postponed until the first time the table is accessed via a db.tablename reference). So, to keep your tables lazy, the preferred method for defining a reference field would be:
That avoids using
db.user, which would immediately force the definition of the user table.Finally, it’s a good idea to add a “format” attribute to tables that are referenced — by default, reference fields use the “format” attribute of the referenced table to determine how to display values of the field in form drop-downs, grids/tables, and read-only forms. For the user table, you could do:
In that case, all fields that reference the user table will display the uname value instead of the underlying record id (even though the reference field itself will store the record id).