[IMP] mail: make inverses optional when unused

IdentifyingFields and fields marked `required` must have their inverse
with `isCausal: true`. This led to having to systematically declare
inverses on the related models in order to explicitly add `isCausal:
true` to them, even when these inverses are not used for anything else.

This commit automatically adds `isCausal: true` to the inverses
generated by the framework when they are related to required fields or
identifyingFields. Thus, it is no longer necessary to declare inverses
if they are not used for anything else.

This commit also removes all the inverses that are no longer needed
thanks to this change.

closes odoo/odoo#81679

Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
This commit is contained in:
Louis Wicket (wil)
2021-12-20 17:59:01 +00:00
parent 6c39b566d6
commit 99b2c61d92
21 changed files with 11 additions and 61 deletions
@@ -638,6 +638,9 @@ export class ModelManager {
) {
throw new Error(`Mismatched relation types: ${field} on ${Model} (${field.relationType}) and ${inverseField} on ${RelatedModel} (${inverseField.relationType}).`);
}
if (field.required && !inverseField.isCausal) {
throw new Error(`${field} on ${Model} is required but its inverse ${inverseField} on ${RelatedModel} is not causal.`);
}
}
for (const identifyingField of Model.__identifyingFieldsFlattened) {
const field = Model.__fieldMap[identifyingField];
@@ -1070,7 +1073,10 @@ export class ModelManager {
relFunc(Model.name, { inverse: field.fieldName }),
{
fieldName: `_inverse_${Model}/${field.fieldName}`,
}
// Note that an identifying field is not necessarily defined as
// `required` (example: identifyingFields with an OR).
isCausal: field.required || Model.__identifyingFieldsFlattened.has(field.fieldName),
},
));
return inverseField;
}