Hi to everyone. I have a many2many filed on which i would like to put a unique constraint. The first class is :
class schoolem_heure_cours_dispense(osv.Model):
_name = 'schoolem.heure_cours_dispense'
_order = 'name'
_columns = {
'name' : fields.char('Nom',required=True,help="Champ automatique"),
'heure_debut' : fields.float('Heure debut',required=True,help="Respecter le format 00:00"),
'heure_fin' : fields.float('Heure fin',required=True,help="Respecter le format 00:00"),
'jour_semaine' : fields.selection([('Lundi','Lundi'),('Mardi','Mardi'),('Mercredi','Mercredi'),('Jeudi','Jeudi'),('Vendredi','Vendredi')],'Jour',required=True),
}
_sql_constraints = [('heure_cours_dispense_uniq','unique(heure_debut,heure_fin,jour_semaine)', 'Une heure de cours avec ces meme valeur existe deja!')]
The second class which causes problem is :
class schoolem_dispense_cours(osv.Model):
_name = 'schoolem.dispense_cours'
_columns = {
'name' : fields.char('Code',required=True,help="Champ automatique"),
'cours_id' : fields.many2one('schoolem.cours','Nom du Cour',required=True),
'aca_id' : fields.many2one('schoolem.aca','Annee Academique',required=True),
'enseignant_id' : fields.many2one('res.users','Enseignant',required=True),
'salle_de_classe_id' : fields.many2one('schoolem.salle_de_classe','Salle de classe',required=True),
'heure_cours_id' : fields.many2many('schoolem.heure_cours_dispense','schoolem_dispense_cours_heure_cours_dispense','dispense_cours_id','heure_cours_dispense_id','Heures de cour'),
#'heure_cours_id' : fields.many2many('schoolem.heure_cours_dispense',required=True),
}
_sql_constraints = [
('dispense_cours_ens_hcours_uniq','unique(aca_id,heure_cours_id,enseignant_id)', "Un enseignant ne peut dispenser deux cours a la meme heure pour cette meme annee academique!"),
('dispense_cours_salle_hcours_uniq','unique(aca_id,heure_cours_id,salle_de_classe_id)', "Un cour a deja ete programme dans cette salle de classe a la meme heure pour cette meme annee academique!")
]
But any of these last 2 constraints are not created in the Database. I guess because the field "heure_cours_id" is a many2many and stored in another table not inside the table "schoolem_dispense_cours".
Please I can I implement these constraints. I thought to a create and write test but don't know how to check if a set (heure_cours_id) contains at leat one value in another set (comming from the form vals) in a domain restriction inside a search method for example.
Thank you in advance.