[FIX] stock: reset product.template records to default type

Stock introduces the option 'product' for the field `type` of the
`product.template` model.

If there exists records for this model with the aforementioned option
selected when the stock module is uninstalled, the records will remain
in the database for an indefinite amount of time, all while pointing to
an option that does no longer exist (except if the record was created by
the stock module itself), making the registry inconsistent and
eventually leading to a crash.

This is a known limitation of the ORM regarding Selection fields and
more specifically the `selection_add` mechanism, no "generic" solution
has been chosen thus far because it is not always clear which approach
should be taken:

    1) Delete the record?
    2) Set the option to a fallback, base option?
    3) Something else handled by the module itself?
    ...

In this case the second approach has been chosen and whenever the module
stock is uninstalled, all remaining product.template records of `type`
'product' will be reset to the default option defined by the field,
which as of this commit is 'consu'.

See opw#2193814

closes odoo/odoo#45341

X-original-commit: a84b2829c5fac9e2b0d8cb49a4e7aa73f7e0e2b2
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
This commit is contained in:
Adrian Torres
2020-02-13 17:13:03 +00:00
parent 416500f6e3
commit b08729267b
2 changed files with 10 additions and 0 deletions
+9
View File
@@ -28,3 +28,12 @@ def _assign_default_mail_template_picking_id(cr, registry):
company_ids_without_default_mail_template_id.write({
'stock_mail_confirmation_template_id': default_mail_template_id.id,
})
def uninstall_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
default = env['product.template']._fields['type'].default(env['product.template'])
# stock introduces an option on the `type` Selection field of `product.template`
# if this module is uninstalled and any `product.template` record still points to this option
# the registry will find itself in an unstable state and will most likely crash (eventually)
cr.execute("UPDATE product_template SET type = %s WHERE type = %s", (default, 'product'))
+1
View File
@@ -88,4 +88,5 @@
'auto_install': False,
'pre_init_hook': 'pre_init_hook',
'post_init_hook': '_assign_default_mail_template_picking_id',
'uninstall_hook': 'uninstall_hook',
}