Hi, I am updating my app and encountered this new database problem:
Basically, my code defines a "criteria" table that refer to auth_user.id so
that each user can set his criteria, or update it.
Old code worked fine:
db.define_table('auth_criteria',
Field <http://127.0.0.1:8001/examples/global/vars/Field>('user_id',
'reference auth_user', readable=False, writable=False),
Field <http://127.0.0.1:8001/examples/global/vars/Field>('salePrice',
'integer', widget=SQLFORM
<http://127.0.0.1:8001/examples/global/vars/SQLFORM>.widgets.radio.widget,
requires = IS_IN_SET
<http://127.0.0.1:8001/examples/global/vars/IS_IN_SET>(salePrice)),
Field <http://127.0.0.1:8001/examples/global/vars/Field>('toSend','integer',
readable=False, writable=False))
db.auth_criteria.user_id.requires = IS_IN_DB
<http://127.0.0.1:8001/examples/global/vars/IS_IN_DB>(db, db.auth_user.id)
db.auth_criteria.id.readable=False
Then I decide to add a new feature. So I added a new column in the table.
Then problem occurs. When new users register and login to set up criteria,
The entry is recorded in the table but no longer *reflected* in the profile
form. So users try to set again and again, but instead of updating the same
auth_criteria entry, they keep generating new lines in the table. New code:
db.define_table('auth_criteria',
Field <http://127.0.0.1:8001/examples/global/vars/Field>('user_id',
'reference auth_user', readable=False, writable=False),
Field <http://127.0.0.1:8001/examples/global/vars/Field>('salePrice',
'integer', widget=SQLFORM
<http://127.0.0.1:8001/examples/global/vars/SQLFORM>.widgets.radio.widget,
requires = IS_IN_SET
<http://127.0.0.1:8001/examples/global/vars/IS_IN_SET>(salePrice)),
Field <http://127.0.0.1:8001/examples/global/vars/Field>('tgPrice',
'integer', widget=SQLFORM
<http://127.0.0.1:8001/examples/global/vars/SQLFORM>.widgets.radio.widget,
requires = IS_IN_SET
<http://127.0.0.1:8001/examples/global/vars/IS_IN_SET>(tgPrice)),
Field <http://127.0.0.1:8001/examples/global/vars/Field>('toSend','integer',
readable=False, writable=False))
db.auth_criteria.user_id.requires = IS_IN_DB
<http://127.0.0.1:8001/examples/global/vars/IS_IN_DB>(db, db.auth_user.id)
db.auth_criteria.id.readable=False
See attachment for screenshot of duplicate entries.
For some reason, this only happens to *new users*, since old user can still
update the entry fine without duplicating.
Can this be fixed by a "unique" attributes to the field? or is it something
wrong I did with "reference"?
Thanks a lot!
-- Yi
My def user():
if request
<http://127.0.0.1:8001/examples/global/vars/request>.args(0)=='profile':
db.auth_criteria.user_id.default = auth.user.id
record = db.auth_criteria(db.auth_criteria.user_id.default)
formFil=SQLFORM
<http://127.0.0.1:8001/examples/global/vars/SQLFORM>(db.auth_criteria,
record=record,
labels = {'salePrice':XML
<http://127.0.0.1:8001/examples/global/vars/XML>('<h3>TWO-A-DAY Clothing and
Home Goods</h3> (By Sale Price)'), 'tgPrice':XML
<http://127.0.0.1:8001/examples/global/vars/XML>('<h3>ONE-A-DAY Travel and Gear
</h3> (By Sale Price)')},
buttons = [TAG
<http://127.0.0.1:8001/examples/global/vars/TAG>.button('Set Mine',
_class='btn-primary')])
if formFil.process().accepted:
response
<http://127.0.0.1:8001/examples/global/vars/response>.flash = XML
<http://127.0.0.1:8001/examples/global/vars/XML>('Your preference is recorded.
<br>Get ready for lovely beans.')
return dict(form=auth(), formFil=formFil)
else:
return dict(form=auth())
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.