unique=True is enforced at the level of the database, not the form. If you want to validate form input, you have to provide a validator in the field's "requires" attribute:
Field('url', unique=True, requires=[IS_NOT_IN_DB(db, 'ads.url'), IS_URL()]) See http://web2py.com/books/default/chapter/29/6#Record-representation and http://web2py.com/books/default/chapter/29/7#Database-validators. Anthony On Friday, January 6, 2012 7:34:10 PM UTC-5, Detectedstealth wrote: > > When using unique=True on my database model the validation is allowing > duplicates to pass through the form validation. Also notnull=False does NOT > allow me to store Null entries for country, province, city. > > db.define_table('ads', > Field('member_id', db. user_account, default=auth.user.account_id), > Field('points', 'integer', default=0), > Field('url', unique=True, requires=IS_URL()), > Field('language', db.languages, requires=IS_IN_DB(db, db.languages.id, > '%(language)s')), > Field('country', db.countries, requires=IS_IN_DB(db, db.countries.id, > '%(name)s'), notnull=False, default=None), > Field('province', db.provinces, requires=IS_IN_DB(db, db.provinces.id, > '%(name)s'), notnull=False, default=None), > Field('city', db.cities, requires=IS_IN_DB(db, db.cities.id, '%(name)s'), > notnull=False, default=None), > Field('accepted', 'boolean', default=False), > Field('viewable', 'boolean', default=True), > Field('updated_at', 'datetime', default=request.now, writable=False), > Field('added_at', 'datetime', default=request.now, writable=False) > ) > > def createad(): > table = ads_api.getDatabaseTable() > table.member_id.readable = table.member_id.writable = False > table.accepted.readable = table.accepted.writable = False > form = SQLFORM(table) > if form.accepts(request,session, keepvalues=True): > response.flash = 'form accepted' > elif form.errors: > response.flash = 'form has errors' > else: > response.flash = 'please fill the form' > return dict(form=form) > > IntegrityError: duplicate key value violates unique constraint "ads_url_key" > DETAIL: Key (url)=(http://wadecybertech.com) already exists. > > > Am I doing something incorrect? I basically want a form that allows country, > province, city to be NULL OR a valid item already in the database, finally I > want the url column to be unique. However when a value already exists and I > submit the form I get the IntegrityError. > > > -- > -- > Regards, > Bruce Wade > http://ca.linkedin.com/in/brucelwade > http://www.wadecybertech.com > http://www.warplydesigned.com > http://www.fitnessfriendsfinder.com >