Skip to content

fix(core): reuse identity-map entries with falsy primary keys - #8757

Merged
B4nan merged 2 commits into
mikro-orm:masterfrom
ragib-mrb:fix/falsy-primary-key-identity-map
Nov 18, 2026
Merged

B4nan merged 2 commits into
mikro-orm:masterfrom
ragib-mrb:fix/falsy-primary-key-identity-map

Conversation

@ragib-mrb

Copy link
Copy Markdown
Contributor

UnitOfWork.tryGetById() treats a falsy primary key as missing, even though Utils.extractPK() returns valid keys such as 0, 0n, and an empty string. As a result, findOne() misses entities already in the identity map: managed entities are queried again, and new entries can be flushed unnecessarily instead of being returned directly.

Check for a nullish key instead. The SQLite regressions cover all three falsy key types, new unflushed entries, managed lookups using scalar/object conditions, and an additional-condition query that must still reach the database.

Validation: all three regressions fail with the old guard. Twenty tests across four relevant SQLite files pass with the fix. Focused strict TypeScript compilation, type-aware oxlint/type checking, formatting, public-content and whitespace checks pass. The full database test matrix was not run.

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.66%. Comparing base (a1fc397) to head (3132f92).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8757   +/-   ##
=======================================
  Coverage   99.66%   99.66%           
=======================================
  Files         294      294           
  Lines       32867    32867           
  Branches     8949     8949           
=======================================
  Hits        32757    32757           
  Misses        106      106           
  Partials        4        4           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@B4nan
B4nan enabled auto-merge (squash) November 18, 2026 12:06
@B4nan
B4nan merged commit 8a9cc31 into mikro-orm:master Nov 18, 2026
23 of 26 checks passed
B4nan pushed a commit that referenced this pull request Nov 23, 2026
…()` (#8806)

With `updateByPrimaryKey` (the default), `assign()` checks `if (pk)`
before matching nested data to the existing related entity, both for
to-one relations and for collection items. When that primary key is `0`
or `''`, the check fails and the data goes through `em.create()`
instead, so the existing entity is not updated and the change is
silently lost on flush.

```ts
const book = await em.findOneOrFail(Book, 1, { populate: ['author'] }); // author.id === 0
wrap(book).assign({ author: { id: 0, name: 'updated' } });
book.author.name; // still the old name
```

This switches both checks to `pk != null`, the same fix #8757 made for
the identity map lookup.
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