With that statement ("""if you don't understand the code, try it""") I
meant that you can always check a functionality testing it before loosing
your mind on the code.
All the "magic" is done by:
gluon/dal.py (validate_and_insert())
...
for key,value in fields.items():
value,error = self[key].validate(value)
if error:
response.errors[key] = error
...
the "validate()" method on the field calls the validators, defined into
gluon/validators.py and "associated" by table definitions (db.define_table)
or field requirements (Field('a', *requires=....*)).
>>> print db.tests.uniquefield.requires
[<gluon.validators.IS_NOT_IN_DB object at 0x32404d0>,
<gluon.validators.IS_LENGTH
object at 0x3240490>]
A unique field gets a IS_IN_DB validator in order to check before inserting
the record (otherwise the commit() would raise an exception at the database
level. Unfortunately, there is no universal way to tell WHICH field failed
(and why) when you blindly insert a record, and you should wait for the
commit() to raise that exception, so you'll lose "multiple" inserts with
their properly related errors).
Some definitions (like the "unique" one) defaults to a "hidden" requires to
your ones (as documented in the book
http://web2py.com/books/default/chapter/29/6#Record-representation).
Datetime fields, for example, are checked with a IS_DATETIME() validator,
reference
with a IS_IN_DB(), etc. etc. etc.
This is done into the sqlhtml_validators() function in gluon/dal.py. You
can find there that a unique Field gets a IS_IN_DB() validator by default
...
if field.unique:
requires._and = validators.IS_NOT_IN_DB(field.db,field)
...
PS: all code excerpts are from the trunk version. May be a little different
from your web2py version, but this is sort of an "introduction" on where to
find answers to your questions.
--