Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
[MERGE][REF] base, *: Compute fields before creation. #42560
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 ...
pre_compute field attribute
Specify fields as
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).
To ensure the new
Until now, stored compute fields were computed after database insertion. This meant that required fields or constraints couldn't be used on computed fields, unless a default was specified. But using a default meant that the field wasn't computed after insertion either, because it was protected against recomputation. #### Pre_compute=False fields We sometimes do want to explicitly compute some computed stored fields AFTER INSERT, mainly: * statistics fields computed with search/read_group/... * fields potentially referencing the current record (e.g. res.partner.commercial_partner_id) * field referencing the first one of a m2m/o2m relational field (only if stored) * fields depending on the create_date/write_date/create_uid/write_uid ... Those fields can be specified as pre_compute=False to force their computation post record creation.
Make sure SQL constraints can apply on compute store fields, even for modules extensions. SQL constraints can apply after module creation for new models, because the new model table is empty (no row to invalidate the constraints). But for extended models, we have to apply SQL constraints (and required) after compute store computation, otherwise the constraint may not be applicable to current records because the compute stored columns are empty (not already computed).