I want to define a table that has a compound field which concatenates
data from two different field values on form submission. In addition,
I would like to check the database on validation to make sure that the
current record is the only to have that "combinada" field value. The
unique argument restriction grants the redundancy is not allowed, but
when duplicated values are sent, the model does not catch database
integrity errors and therefore an exception is raised, at least that
is what I assume so far. How could I catch the db exception at
validation instead of having to handle an adapter related error (which
in turn will probably work only for the current database adapter)?
The prototype is as follows (using the default sqlite uri).
Model:
db.define_table("combinada", Field("combinado",
requires=IS_NOT_EMPTY(), compute=lambda row: row.primero+row.segundo,
unique=True, writable=False), Field("primero",
requires=IS_NOT_EMPTY()), Field("segundo", requires=IS_NOT_EMPTY()))
Here is the error traceback stored by web2py:
Traceback (most recent call last):
File "/home/alan/web2py/web2py-hg/gluon/restricted.py", line 204, in
restricted
exec ccode in environment
File "/home/alan/web2py/web2py-hg/applications/compute/controllers/
default.py", line 73, in <module>
File "/home/alan/web2py/web2py-hg/gluon/globals.py", line 172, in
<lambda>
self._caller = lambda f: f()
File "/home/alan/web2py/web2py-hg/applications/compute/controllers/
default.py", line 18, in index
form = crud.create(db.combinada)
File "/home/alan/web2py/web2py-hg/gluon/tools.py", line 3168, in
create
formname=formname,
File "/home/alan/web2py/web2py-hg/gluon/tools.py", line 3111, in
update
detect_record_change = self.settings.detect_record_change):
File "/home/alan/web2py/web2py-hg/gluon/sqlhtml.py", line 1273, in
accepts
self.vars.id = self.table.insert(**fields)
File "/home/alan/web2py/web2py-hg/gluon/dal.py", line 6634, in
insert
return self._db._adapter.insert(self,self._listify(fields))
File "/home/alan/web2py/web2py-hg/gluon/dal.py", line 934, in insert
raise e
IntegrityError: column combinado is not unique