From 8e577e93940d1e9d4bf6e182802451ecf00e5bbe Mon Sep 17 00:00:00 2001 From: Eric Antones Date: Tue, 7 Jul 2026 10:58:27 +0200 Subject: [PATCH] [OU-FIX] repair: fallback picking type for repairs outside warehouses fill_repair_picking_type() maps repair orders to their warehouse's new repair picking type by joining repair_order.location_id -> stock_location.warehouse_id. Repairs whose location does not belong to any warehouse (standalone workshop locations, customer/transit/virtual locations) are left referencing the temporary location and picking type created in pre-migration, and the cleanup unlink crashes: the FK ON DELETE SET NULL collides with the NOT NULL constraint the ORM adds for these required fields. Add a per-company fallback that assigns the first active warehouse's repair type to the remaining rows before deleting the temporary records. --- .../scripts/repair/17.0.1.0/post-migration.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/openupgrade_scripts/scripts/repair/17.0.1.0/post-migration.py b/openupgrade_scripts/scripts/repair/17.0.1.0/post-migration.py index ed490b75b4c1..a01b4980f704 100644 --- a/openupgrade_scripts/scripts/repair/17.0.1.0/post-migration.py +++ b/openupgrade_scripts/scripts/repair/17.0.1.0/post-migration.py @@ -207,6 +207,43 @@ def fill_repair_picking_type(env): WHERE ro.location_id = sl.id """, ) + # OU-FIX: the UPDATE above only covers repairs whose location_id belongs + # to a warehouse (stock_location.warehouse_id). Repairs on standalone + # locations (workshop locations outside any warehouse hierarchy, + # customer/transit/virtual locations) keep referencing the temporary + # location/picking type created in pre-migration, and the unlink below + # crashes: the FK ON DELETE SET NULL collides with the NOT NULL the ORM + # added for these required fields. Fall back per company to its first + # active warehouse's repair type. + tmp_picking_types = env["stock.picking.type"].search( + [("name", "=", "SPT Repair OpenUpgrade")] + ) + if tmp_picking_types: + openupgrade.logged_query( + env.cr, + """ + UPDATE repair_order ro + SET picking_type_id = sub.repair_type_id, + location_dest_id = sub.default_location_dest_id, + parts_location_id = sub.default_remove_location_dest_id, + recycle_location_id = sub.default_recycle_location_dest_id + FROM ( + SELECT DISTINCT ON (sw.company_id) + sw.company_id, + sw.repair_type_id, + spt.default_location_dest_id, + spt.default_remove_location_dest_id, + spt.default_recycle_location_dest_id + FROM stock_warehouse sw + JOIN stock_picking_type spt ON spt.id = sw.repair_type_id + WHERE sw.repair_type_id IS NOT NULL + ORDER BY sw.company_id, sw.active DESC, sw.id + ) sub + WHERE sub.company_id = ro.company_id + AND ro.picking_type_id IN %(tmp_ids)s + """, + {"tmp_ids": tuple(tmp_picking_types.ids)}, + ) # Remove the temporary location and picking type # created during the migration env["stock.location"].search(