Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
4 ตอบกลับ
6752 มุมมอง

Hello,

This is my model :

class miadi_poidsConditionnement(models.Model):

 _name = 'miadi.poidsconditionnement'

 # _order = 'conditionnement_id asc'

 conditionnement_id = fields.Many2one('miadi.packaging', 'Packaging', default='', required=True)

 produit_id = fields.Many2one('product.product', 'Product', default='', required=True)

 nb_articles = fields.Integer(string='Number of products', default=0)

 poids = fields.Float(string='Packaging Weight', default=0)

 _sql_constraints = [

 ('uniq_id', 'unique(produit_id, conditionnement_id)', 'A product already exists with this packaging !'),

 ]

But my SQL constraints don't work ! I can create 2 records with the same "conditionnement_id" and "produit_id" but I shouldn't be able to do it.


อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

# class miadi_poidsConditionnement(models.Model):
                _sql_constraints    =    [   
                                ('uniq_id',   
                                    'UNIQUE (produit_id, conditionnement_id)',

                                    'A product already exists with this packaging !')]

Try this

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

check if there is any duplication before applying the constrains , if any one found then i won't take any effect , remove the duplication first then install/upgrade the module

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

SQL constraints works only for New Records, so you may need to test it in new Database.

อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

Hi everybody,

I fix the problem unistalling the module and install it again

อวตาร
ละทิ้ง