Hi,
My requirement as brief:
- Root contact is a contact whose parent_id is None
- I don't want to have *root contacts* with the same email/country
- I need to use sql_constraints to fix this issue
- I need to define a new field named root_contact for all contacts and set it to 0 or 1 for all contacts
So,
I've defined root_contact as:
root_contact = fields.Integer(
string="Root Contact",
compute="_compute_root_contact",
copy=False,
store=True
)
And the function is:
def_compute_root_contact(self):
if(self.parent_id):
self.root_contact = 0
else:
self.root_contact = 1
As there are many previous contacts in the contact list, how can I set this field for all contacts?
Later, I want to set sql_constraints as below:
_sql_constraints = [
('email_country_parent_uniq', 'UNIQUE(root_contact,email,country_id)',
'A root contact with the same info is presented before. \
Please edit that contact or define a new one with different info.')
]
Problems:
- If I use store=True the compute function doesn't call at all, and in this case, based on the Odoo document, I need to set @api.onchange to be able to call this method (and this is not the thing I want).
- If I don't use store=True so the field doesn't store in the database and so, I could not use the field in sql_constraints
From Postgres 15, a new feature is added named 'NULLS NOT DISTINCT' that can be used with the combination of UNIQUE in sql_constraints and this is the best-fit choice for my requirement but I'm using Postgres 14.