If inserts into the table will be done via form submission and you don't
want to store an extra field in the table, another option is:
db.define_table('letter',
Field('name'),
Field('location',
requires=IS_NOT_IN_DB(db(db.letter.name==request.vars.name),
'letter.location')))
That will make sure location is unique among the set of records where name
matches the new value of name being inserted (so the combination of name
and location must be unique). It depends on the value of name being
available in request.vars, so as is, this method will only work with
form-based inserts -- otherwise, you could do:
db.letter.location.requires = IS_NOT_IN_DB(db(db.letter.name==my_new_name),
'letter.location')
result = db.letter.validate_and_insert(name=my_new_name, location=
my_new_location)
if result.errors:
print result.errors
Another option with form submissions is to use an onvalidation function:
http://web2py.com/books/default/chapter/29/7#onvalidation.
Anthony
On Thursday, May 10, 2012 6:36:18 AM UTC-4, rochacbruno wrote:
>
> db.define_table('nm', Field('a', notnull=True), Field('b', notnull=True),
> Field('unikey', unique=True))
>
> db.nm.unikey.compute = lambda row: row.a + row.b
> Em 10/05/2012 02:46, "Alec Taylor" <[email protected]> escreveu:
>
>> E.g.: For a letter to be unique and identifiable it needs unique(location
>> && name)
>>
>> How do I make a requirement that a combination of fields must be unique?
>>
>> Thanks for all information,
>>
>> Alec Taylor
>>
>