Skip to content

[19.0][FIX] stock_account: fix syntax error in stock_move_value() post-migration on PostgreSQL 13 - #5903

Merged
pedrobaeza merged 1 commit into
OCA:19.0from
zbynekdrlik:fix/stock-account-19-post-migration-value-alias
Aug 11, 2026
Merged

[19.0][FIX] stock_account: fix syntax error in stock_move_value() post-migration on PostgreSQL 13#5903
pedrobaeza merged 1 commit into
OCA:19.0from
zbynekdrlik:fix/stock-account-19-post-migration-value-alias

Conversation

@zbynekdrlik

Copy link
Copy Markdown
Contributor

Bug

stock_move_value() in openupgrade_scripts/scripts/stock_account/19.0.1.1/post-migration.py
builds an aggregate subquery with a bare-word column alias (no AS) immediately
after the sum() call:

UPDATE stock_move
SET value=aggregated_values.value
FROM (
    SELECT
    move_id, sum(value) value
    FROM
    product_value
    GROUP BY move_id
) aggregated_values
WHERE aggregated_values.move_id=stock_move.id

PostgreSQL 13 rejects this as a syntax error:

ERROR:  syntax error at or near "value"
LINE 5:     move_id, sum(value) value
                                ^

This is PostgreSQL-version dependent, not data-dependent — the same statement
runs fine on PostgreSQL 16 (confirmed below), which makes the bug easy to miss
when re-testing on a newer engine. Any 18.0 -> 19.0 migration of a database
with stock_account installed, run against a PostgreSQL 13 backend, hits this
unconditionally partway through the post-migration step.

Repro (real, not assumed)

Against a PostgreSQL 13.23 database used for an actual 15.0 -> 19.0
OpenUpgrade migration chain, inside BEGIN; ... ROLLBACK; so nothing persisted:

BEGIN;
UPDATE stock_move
SET value=aggregated_values.value
FROM (
    SELECT
    move_id, sum(value) value
    FROM
    product_value
    GROUP BY move_id
) aggregated_values
WHERE aggregated_values.move_id=stock_move.id;
ROLLBACK;

BEGIN
ERROR:  syntax error at or near "value"
LINE 5:     move_id, sum(value) value
                                ^
ROLLBACK

The same statement, patched (agg_value alias), against the same database,
in the same rollback-guarded transaction:

BEGIN
UPDATE 3
ROLLBACK

A separate check against a PostgreSQL 16 database did not reproduce the
error — confirming this is version-dependent, not something wrong with the
data.

Fix

Alias the aggregate column explicitly (agg_value) and reference the aliased
name in the outer UPDATE ... SET, instead of relying on a bare column name
that happens to collide with the outer table's own value column:

 UPDATE stock_move
-SET value=aggregated_values.value
+SET value=aggregated_values.agg_value
 FROM (
     SELECT
-    move_id, sum(value) value
+    move_id, sum(value) AS agg_value
     FROM
     product_value
     GROUP BY move_id
 ) aggregated_values
 WHERE aggregated_values.move_id=stock_move.id

Test

python3 -m py_compile on the patched file compiles clean. ruff check /
ruff format --check on the patched file both pass with no findings. The
patched SQL statement was run against the same PostgreSQL 13.23 database
used for the repro above (again inside BEGIN;...ROLLBACK;) and completed
with UPDATE 3, no error.

No functional/behavioral test suite covers this migration script beyond
OpenUpgrade's own migration-execution smoke test, so this fix was verified
by direct SQL execution against a real PostgreSQL 13 database reproducing
the exact failure, per the repro above — not merely reasoned about.

…ation on PostgreSQL 13

stock_move_value() in stock_account/19.0.1.1/post-migration.py builds an
UPDATE ... FROM (SELECT ... sum(value) value ...) aggregate subquery with
a bare-word column alias (no AS) immediately after the aggregate call.

PostgreSQL 13 rejects this as a syntax error:

    ERROR:  syntax error at or near "value"
    LINE 5:     move_id, sum(value) value
                                    ^

PostgreSQL 16 accepts the same statement, which is why this is easy to
miss when re-testing on a newer engine — it is PostgreSQL-version
dependent, not data-dependent. Any 18.0 -> 19.0 migration of a database
with stock_account installed, run against a PostgreSQL 13 backend, hits
this unconditionally partway through the post-migration step.

Fix: alias the aggregate column explicitly (agg_value) and reference the
aliased name in the outer UPDATE ... SET, instead of relying on the bare
column name colliding with the outer table's own value column.

Found and reproduced during a real 15.0 -> 19.0 OpenUpgrade migration
(PostgreSQL 13 source engine). Confirmed the exact failing statement
against a live PostgreSQL 13.23 database inside a BEGIN;...ROLLBACK; and
confirmed the patched statement runs clean against the same database.
@pedrobaeza pedrobaeza added this to the 19.0 milestone Aug 11, 2026

@pedrobaeza pedrobaeza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although using so old PG version in 19 is not recommended, this works for all the PG versions, so it's harmless.

@pedrobaeza
pedrobaeza merged commit 29cc4e3 into OCA:19.0 Aug 11, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants