This question has been flagged
1 Reply
4020 Views

I need to add sql constraint so that it prevent the duplication in customer name.The sql constraint cannot be added to the existing module as it cannot be edited.So a new module has to be created.How to create this module and how to inherit the features in my new module and add the new features. ?

Avatar
Discard
Best Answer

Hi,

You can add the following code in your module to apply unique constraint for Customer :

class res_partner(osv.osv):
    _inherit = 'res.partner'

    _sql_constraints = [
        ('name_uniq', 'unique(name)', 'Customer Name must be unique!'),
    ]

Before installing the module, make sure that existing customers don't have duplicate names. If exists, rename those Customers, else constraints will not be applied.

Avatar
Discard
Author

How can a validation for the customer name be linked with the sale_order class?This should be inherited with res.partner isn't it?

I have updated my answer.

Author

so can i add the _sql_constraints to the default file that contains the class?

Instead of adding this in default file, better to create new module. You just need to create module with _init_.py , _openerp_.py and py file which includes the given code. This will add the constraint for Partner name.

Author

what about the code in _init_.py and _openerp_.py files?

Author

Isn't .xml file necessary in the new module?

Author

I installed a new module customer_validation for the above purpose and added the code in py file but now the issue is the validation does not work.Still I can add a name and save it which already exists.

Before installing the module, make sure that existing customers don't have duplicate names. If exists, rename those Customers, else constraints will not be applied.

Author

i had duplicate names when I installed the module.So i am going to uninstall the module,remove the duplicate names and then install the module again.Is that ok?

Yes it is ok if by upgrading module constraint is not applied.

Author

I tried both the ways.First by upgrading the module and then by uninstalling and then reinstalling it.But still the name validation is not working.