validate_and_update : I don't know what's going on ?

61 views
Skip to first unread message

Pierre

unread,
May 30, 2016, 9:34:48 AM5/30/16
to web2py-users
Hi,

here it is : the cruel reality as is          :(

(1) the table:

db.define_table('uperson',
               
Field('auth_user','reference auth_user',unique=True,requires = IS_NOT_IN_DB(db,'uperson.auth_user')),
               
Field('country'),
               
Field('age','integer'))



(2) the actions:

>>> db.uperson.insert(auth_user=1,age=100,country='fr')
1L
>>> db.commit()
>>> r = db.uperson.validate_and_update(db.uperson.auth_user==1,age=101)
Traceback (most recent call last):
 
File "<console>", line 1, in <module>
 
File "/opt/web2py/gluon/packages/dal/pydal/objects.py", line 741, in validate_and_update
    response
, new_fields = self._validate_fields(fields, 'update')
 
File "/opt/web2py/gluon/packages/dal/pydal/objects.py", line 727, in _validate_fields
    value
, error = self[fieldname].validate(raw_value)
 
File "/opt/web2py/gluon/packages/dal/pydal/objects.py", line 1625, in validate
   
(value, error) = validator(value)
 
File "/opt/web2py/gluon/validators.py", line 696, in __call__
    row
= subset.select(table._id, field, limitby=(0, 1), orderby_on_limitby=False).first()
 
File "/opt/web2py/gluon/packages/dal/pydal/objects.py", line 2002, in select
   
return adapter.select(self.query,fields,attributes)
 
File "/opt/web2py/gluon/packages/dal/pydal/adapters/sqlite.py", line 122, in select
   
return super(SQLiteAdapter, self).select(query, fields, attributes)
 
File "/opt/web2py/gluon/packages/dal/pydal/adapters/base.py", line 1273, in select
    sql
= self._select(query, fields, attributes)
 
File "/opt/web2py/gluon/packages/dal/pydal/adapters/base.py", line 1160, in _select
    sql_w
= ' WHERE ' + self.expand(query) if query else ''
 
File "/opt/web2py/gluon/packages/dal/pydal/adapters/base.py", line 945, in expand
    rv
= op(first, second, **optional_args)
 
File "/opt/web2py/gluon/packages/dal/pydal/adapters/base.py", line 839, in EQ
   
self.expand(second, first.type))
 
File "/opt/web2py/gluon/packages/dal/pydal/adapters/base.py", line 955, in expand
    rv
= self.represent(expression, field_type)
 
File "/opt/web2py/gluon/packages/dal/pydal/adapters/base.py", line 1432, in represent
   
return str(long(obj))
ValueError: invalid literal for long() with base 10: 'None'

second attempt
:

>>> db.uperson.validate_and_update(db.uperson.auth_user==1,auth_user=1,age=102)
<Row {'errors': {'auth_user': 'Value already in database or empty'}, 'id': None}



Niphlod

unread,
May 30, 2016, 10:15:39 AM5/30/16
to web2py-users
noooope!

validate_and_update is a method of a Query, not of a Table! (you may have scrambled it with validate_and_insert()...)

What you need to do is

db(db.uperson.auth_user==1).validate_and_update(age=101)

Pierre

unread,
May 30, 2016, 11:21:44 AM5/30/16
to web2py-users
great !!! works
now I can validate all users    even the old ones    :)


thanks Niphlod
Reply all
Reply to author
Forward
0 new messages