From 88d200b1b7f46a4811fad64de22ab17ac32c21a6 Mon Sep 17 00:00:00 2001 From: Muhammad Ali Date: Tue, 1 Sep 2026 12:32:08 +0500 Subject: [PATCH] [OU-FIX] hr_contract: fill contract_id for every employee, not just one fill_employee_contract_id() built the backfill values in a non-correlated subquery with LIMIT 1 and no ORDER BY. Postgres evaluates that subquery once, independent of the outer UPDATE, so LIMIT 1 returned a single arbitrary (employee_id, contract_id) pair across the whole database instead of one per employee. Every other employee with a genuinely 'open' contract was left with contract_id = NULL, which then falsely triggers hr.employee.contract_warning post-migration. Replace the non-correlated LIMIT 1 with DISTINCT ON (employee_id) ORDER BY employee_id, id so exactly one contract per employee is picked deterministically and every qualifying employee row is updated. --- addons/hr_contract/migrations/13.0.1.0/post-migration.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/addons/hr_contract/migrations/13.0.1.0/post-migration.py b/addons/hr_contract/migrations/13.0.1.0/post-migration.py index af7d4e399ee9..72f613e9fe2d 100644 --- a/addons/hr_contract/migrations/13.0.1.0/post-migration.py +++ b/addons/hr_contract/migrations/13.0.1.0/post-migration.py @@ -28,10 +28,11 @@ def fill_employee_contract_id(env): UPDATE hr_employee he SET contract_id = sub.contract_id FROM ( - SELECT he.id AS employee_id, hc.id as contract_id - FROM hr_contract hc, hr_employee he - WHERE he.id = hc.employee_id AND hc.state = 'open' - LIMIT 1 + SELECT DISTINCT ON (hc.employee_id) + hc.employee_id, hc.id AS contract_id + FROM hr_contract hc + WHERE hc.state = 'open' + ORDER BY hc.employee_id, hc.id ) sub WHERE sub.employee_id = he.id AND he.contract_id IS NULL """