Repository navigation
fix(persistence): keep many-to-many junction rows when the relation is not loaded - #12711
Merged
alumni merged 1 commit intoJul 29, 2026
Conversation
…t loaded A batch save() (or cascade) deleted every junction row of an entity whose many-to-many relation property was undefined, whenever another entity in the same persistence group had that relation loaded. Restore the undefined-means-skip guard that was accidentally dropped when the null check was converted to nullish coalescing, mirroring the guard OneToManySubjectBuilder kept in the same refactor. Fixes typeorm#12710
commit: |
Cprakhar
approved these changes
Jul 18, 2026
alumni
approved these changes
Jul 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #12710
Description of change
Saving several entities in one
save()call (or via a cascade) silently deleted every many-to-many junction row of any entity in the batch whose relation property was not loaded, whenever another entity in the same persistence group had that relation loaded.Root cause: the
prefer-nullish-coalescingstyle refactor (cab8fcb, #12340) changedManyToManySubjectBuilder.buildForSubjectRelationfromto
??=also matchesundefined, so an unloaded relation — which previously fell through to the!Array.isArrayguard and was skipped — became "relation set to empty, remove all junction rows". The removal only materializes when another subject in the same group has the relation loaded (that makesSubjectDatabaseEntityLoaderfetch the database-side relation ids for the whole target group), which is why single-entity saves look fine and the data loss surfaces on batch saves and cascades. The same refactor kept the explicitundefined → returnguard in the siblingOneToManySubjectBuilder— only the many-to-many builder lost it.Fix: restore the
undefined → skipguard, mirroringOneToManySubjectBuilder.nullstill means "remove all bindings" and non-array values (e.g. lazy-relation promises) are still skipped, so the documented 1.0 semantics are all preserved:undefined(not loaded) → junction rows left intact (this was broken)null/[]→ all junction rows removed (unchanged, still covered by the existing tests in the same file)Verification:
test/functional/persistence/many-to-many/persistence-many-to-many.test.ts: two posts saved in one call, one loaded with its categories and one without; asserts the unloaded one keeps its junction row. It fails onmaster(the junction row is deleted) and passes with this fix.Pull-Request Checklist
masterbranchFixes #NNNN,Closes #NNNN, orResolves #NNNN— Fixes Batch save() silently deletes many-to-many junction rows of entities saved without their relation loaded (v1.0 regression) #12710tests/**.test.ts)docs/docs/**.md) — N/A (restores the documented behavior from the 1.0 upgrade guide; no documented behavior changes)