mysql - how to set a multi-column unique in web2py -


in order make many many relation ship make middle table combine 2 tables ,the tables this:

db.define_table('problem',                 field('task_id','reference task'),                 field('title','string',unique=true,length=255)) db.define_table('task',                 field('title','string',unique=true,length=255),                 field('course_id','reference courses'))  db.define_table('belong',                 field('task_id','reference task'),                 field('problem_id','reference problem')                 ) db.belong.task_id.requires=is_in_db(db,'task.id','%(title)s') db.belong.problem_id.requires=is_in_db(db,'problem.id','%(title)s') 

i use sqlform insert belong table.i want there no duplicate task , problem. suppose there record (1,task1,problem1)already exist in belong table,then if choose task1 in sqlform,how make problem_id dropdown list not show problem1. have found similar question here , test following in db.py:

test1:    db.belong.problem_id.requires=is_not_in_db(db(db.belong.task_id==request.vars.task_id),'belong.problem_id').  test2:  db.belong.problem_id.requires=is_not_in_db(db(db.belong.task_id==request.vars.task_id),'problem.id','%(title)s'). 

test3:

def my_form_processing(form):     = form.vars.task_id     b=form.vars.problem_id     query=(db.belong.task_id==a)&(db.belong.problem_id==b)     if query :         form.errors.a= 'the record has existed'  def assignproblem():     form=sqlform(db.belong)     if form.process(onvalidation=my_form_processing).accepted:         response.flash='form accepted'     elif form.errors:         response.flash='form has errors'     else:         response.flash='please fill out form'     return dict(form=form) 

but still not been solved.

if want choices in problem_id dropdown change dynamically based on task_id selection, need use javascript (and make ajax request populate dropdown). possible solutions, see this answer.

as tests above, test1 should proper validation, after form has been submitted (i.e., form allow combination of task_id , problem_id selected report error if duplicate combination has been submitted).

test3 not work because query object specifies database query without doing select database. instead, must call .select() method, or more simply, .count() method:

if db((db.belong.task_id == a) & (db.belong.problem_id == b)).count():     form.errors.a= 'the record has existed' 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -