On Tuesday, October 11, 2011 4:04:41 AM UTC-4, spyker wrote: > > I have several tables where there should only be one possible combination > of two uuid-fields per table (mostly many to many links). > > I have created 'unique' constraints for those fields in Postgresql so the > database would not accept any inserts or edits which violates that > constraint. > > I use that in combination with 'try:.. except:' to insert data. > > Can DAL be used in a way for this? As far as I understand the IS_NOT_IN_DB > validators that it is working on form-level and not on database level and I > am not sure whether it can be used for a combination of fields (unless I do > it with a computed field - which will use up a lot of extra resources).
If you only need to validate form submissions (and not other means of making inserts), then you could do something like: db.define_table('mytable', Field('field1'), Field('field2', requires=IS_NOT_IN_DB(db(db.mytable.field1==request.vars.field1), 'mytable.field2'))) That will make sure field2 is unique among the set of records where field1 matches the new value of field1 being inserted (so the combination of field1 and field2 must be unique). It depends on the values being available in request.vars, so not relevant for non-form inserts (though in that case, I suppose you could create your own request.vars as a hack). Anthony