If you want to catch SQL constraints violations in OpenERP/Odoo controllers this tutorial will help you,
So, You can add an SQL constraint to Odoo model without facing any difficulty, to do this we have to use the _sql_constraints
list. Odoo Forms handle those constraints automatically in pgadmin.
let's consider the below example:
class ShivaController(http.Controller):
@http.route('/Shiva/create/', auth='public', website=True)
def create(self, foo, bar):
try:
http.request.env['my.Shiva'].create({
'foo': foo,
'bar': bar,
})
return http.request.render('my.thank_you_page')
except IntegrityError:
http.request._cr.rollback()
return http.request.render('my.error_page')
Note- In SQL constraints current transaction is aborted, commands ignored until end of transaction block.
And after changing SQL constraints we have to inherit SQL Constraints from odoo 7 moduels to Odoo 8, see the example shown below:
class Findnerd(models.Model):
_inherit = 'my.Findnerd '
# ...
_sql_constraints = [
('name_uniq', 'unique(name)', 'Two foos with the same name? Impossible!')
]
def _auto_init(self, cr, context=None):
self._sql_constraints = self._sql_constraints.reverse()
super(Findnerd, self)._auto_init(cr, context)
0 Comment(s)