Friday, November 26, 2021
1 change · master
Code cleanup and technical improvements
Odoo now calculates certain stored fields before a record is first saved, making it possible to require those values and enforce database rules on them. This improves data reliability across core models while keeping exceptions for fields that still need to be calculated after creation.
Original PR description
Currently, the computed fields are computed after record creation in DB. This means that computed fields cannot be required and that no SQL constraints can be based on those fields. To support…
Currently, the computed fields are computed after record creation in DB. This means that computed fields cannot be required and that no SQL constraints can be based on those fields. To support required and sql constraints on computed (and stored) fields, we need to compute the fields before record creation ... ### Side features/changes ##### `_pre_compute` model attribute Specify a model as `_pre_compute=True` to enable computed fields computation before record creation. Added to make the feature OPT-IN, allowing us to convert models one by one when there is a gain to pre_compute their computed fields. ##### `pre_compute` field attribute Specify fields as `pre_compute=False` to ensure they are computed after record creation * Done by default if any compute dependency is `pre_compute=False` * Done by default if field depends on `create_date/write_date/create_uid/write_uid` * Done manually for part of existing fields (mainly statistics fields) ##### Postpone NOT NULL AND SQL constraints after fields/models computation/setup Column are set to NOT NULL after fields computation in DB to ensure model extension with required computed fields works fine. ##### New Record cannot impact real records And inversely To pre-compute computed stored fields, we instantiate a new record with the values given to the create call. This meant that two records were put in cache for one creation (one real and one new). When a record is created linking to real records in o2m or m2m fields, the inverse was triggered and you could have new records in the relational fields of real records. Example: Create an `account.move.line`, linking to real `account.move` 1, if you accessed the lines from the `account.move`, you would receive 2 records (one new and one real). To ensure the new `new()` call in the `create()` method doesn't impact real records, the inverse of new (resp. real) records changes is not applied on real (resp. new) records anymore. It was already the strategy of the orm, but is more strictly applied from now on. -- I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr