Skip to content

Fix bulk shift ordering to prevent duplicate key violations on MySQL#508

Open
jasper-vandemalle wants to merge 2 commits intodatanucleus:masterfrom
jasper-vandemalle:bulk-shift-ordering
Open

Fix bulk shift ordering to prevent duplicate key violations on MySQL#508
jasper-vandemalle wants to merge 2 commits intodatanucleus:masterfrom
jasper-vandemalle:bulk-shift-ordering

Conversation

@jasper-vandemalle
Copy link

@jasper-vandemalle jasper-vandemalle commented Feb 23, 2026

Fixes #464.

When shifting list indices upward (inserting before the end of a list), MySQL processes the bulk UPDATE ... SET idx = idx + 1 in ascending primary key order. The row at the lowest index gets updated first, and its new value collides with the next row's current index.

This adds ORDER BY idx DESC to the bulk shift statement for positive shifts, so the highest-index row moves first and each update lands in a vacant slot. Negative shifts (from remove operations) already work correctly with ascending order.

ORDER BY in UPDATE is a MySQL/MariaDB extension not supported by PostgreSQL, Oracle, or SQL Server, so this is gated behind a new ORDER_BY_IN_UPDATE_STATEMENT adapter capability. MySQL and H2 register it; other databases keep the existing behavior.

Also fixes a minor bug in the internalShiftBulk catch block that referenced shiftStmt instead of the local shiftBulkStmt.

Also included an additional integration test:

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.

BatchUpdateException when reordering elements of 1-m relationship as List<> (join table) - with testcase

1 participant