Skip to Content
Menu
This question has been flagged
2 Replies
5130 Views

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.




Avatar
Discard
Best Answer

Hello Albert

In your database in duplicate value already exist, first remove duplicate data and then restart server it will work.

Avatar
Discard
Author

Thank you very much for answering my post. But you seem to misunderstand my problem. I did not meet duplicate data issue which will tell me “unable to add constraint xxx". My problem is that the constrain is stated, and system does not give me “unable to add constraint xxx" message, but when I set a negative value to a field for which a positive check wast set, but the server accept it. So I use psql to check the database, and find that the positive check is not set at all. However I do have the following in my python code:
_sql_constraints = [
('check_positive', 'CHECK(expected_price > 0 )',
'The price shall be positive!')
]
And it works the first it is set but with another condition. In short, I mean when I made a modification to positive check , my modification does not effect. If I drop constraint using psql, and restart the server, I even do not find positive check at all. So my conclusion is that when a check is modified odoo may not make the modification change to a proper psql command. I hope someone can correct me by doing the same experiment but result in a different result, so that I can know what is wrong with my experiment.

Best Answer

For anyone having this issue.

Check values that are already stored in DB. 
in my case i was setting a constraint to check if price was a positive number.

I used (price > 0)
My database had data where the price was 0 this caused the constraint to not be applied.
after removing that data and restarting the server, the constraint was set and everything worked properly.

Avatar
Discard
Related Posts Replies Views Activity
0
Aug 21
2525
2
Mar 24
8370
1
Feb 24
1392
2
Dec 23
2525
0
Jun 21
2304