I am still searching for an elegant multi column unique constraint
solution.
My model reads like:
db=SQLDB('sqlite://annet.db')
db.define_table('shape',
SQLField('name'),
migrate='shape.table')
db.define_table('color',
SQLField('name'),
migrate='color.table')
db.define_table('shapecolor',
SQLField('shapename'),
SQLField('colorname'),
SQLField('shapecolor'),
migrate='shapecolor.table')
db.shapecolor.shapename.requires=IS_IN_DB(db,
db.shape.name,'%(name)s')
db.shapecolor.colorname.requires=IS_IN_DB(db,
db.color.name,'%(name)s')
db.shapecolor.shapecolor.requires=IS_NOT_IN_DB
(db,'shapecolor.shapecolor')
When, in appadmin I insert circle blue and manually add circleblue
twice the validator works. So, I wrote the following function in the
default controller:
def index():
from gluon.sqlhtml import form_factory
form=SQLFORM(db.shapecolor,fields=['shapename','colorname'])
form.vars.shapecolor=str(form.vars.shapename)+str
(form.vars.colorname)
if form.accepts(request.vars,session):
response.flash='form accepted'
elif form.errors:
response.flash='form has errors (a shape with this color
already exists)'
else:
response.flash='please fill the form'
return dict(form=form)
When I expose the function and insert circle green twice the validator
doesn't work. When I take a look in database db select the shapecolor
column contains NoneNone twice, so
form.vars.shapecolor=str(form.vars.shapename)+str(form.vars.colorname)
results in NoneNone instead of circlegreen, and the validator doesn't
work, it inserts NoneNone and NoneNone without complaining.
I hope it is possible to implement my idea in web2py, and that one of
you tells me how to code this correctly.
Best regards,
Annet.