When I am doing the exercise of Chap 11 of "Getting Started " of "Tutorials" of "Developer" (https://www.odoo.com/documentation/14.0/developer/howtos/rdtraining/11_constraints.html). I find very puzzling phenomena. In my python program, I have the following codes:
class estate_estate(models.Model):
title = fields.Char('Title', required=True, translate=True)
selling_price = fields.Float('Selling Price', readonly=True, copy=False)
expected_price = fields.Float('Expected Price', copy=False)
_sql_constraints = [
('check_positive', 'CHECK(expected_price > 0 AND selling_price >0 )',
'The price shall be positive!')
]
_sql_constraints = [
('check_uniqueness', 'unique(title)', 'The property name shall be unique !')
]
At first, the uniqueness check work properly, but the check_positive works partially correct. So I modify the constraints to
_sql_constraints = [
('check_positive', 'CHECK(expected_price > 0 )',
'The price shall be positive!')
]
But when I use psql to check constraints, I find that the constraint is still the original one, i.e. CHECK(expected_price > 0 AND selling_price >0. So I use the following to drop constraints
ALTER TABLE estate_estate DROP CONSTRAINT check_positive;
I do find no more constrains on table, but when I use command to reactivate odoo./odoo/odoo-bin --addons-path=./custom,./odoo/odoo/addons -d rd-demo -u estate_estate --dev xml
I still did not see the check_positive constraint using psql, but check_uniqueness is still there:
Indexes:
"estate_estate_check_uniqueness" UNIQUE CONSTRAINT, btree (title)
I do an experiment by removing check_uniqueness and restart odoo, then I add a duplicate property yyy to see if there is warning message, interestingly, I find that the user exception handling message 'The property name shall be unique !' did disappear, but in its stead is the system message:
Validation Error
×duplicate key value violates unique constraint "estate_estate_check_uniqueness" DETAIL: Key (title)=(yyy) already exists.
I use psql commands to double check, see what I get?"estate_estate_check_uniqueness" UNIQUE CONSTRAINT, btree (title)
The check_uniqueness is still there.
So, my conclusion seems to be: when adding _sql_constraints for the first time, it works. But then you make some modification of the statement and restart odoo, those new settings seems to be no longer effective.
This is very baffling. Help is desperately needed.