Skip to content

fix(core): update related entities with falsy primary keys in assign() - #8806

Merged
B4nan merged 2 commits into
mikro-orm:masterfrom
Johannes7549:fix/assign-falsy-primary-keys
Nov 23, 2026
Merged

B4nan merged 2 commits into
mikro-orm:masterfrom
Johannes7549:fix/assign-falsy-primary-keys

Conversation

@Johannes7549

Copy link
Copy Markdown
Contributor

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.

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.

With `updateByPrimaryKey`, `assign()` only matched a nested object to the
existing related entity when its primary key was truthy. For a key of `0`
or `''` the data was treated as a new entity instead, so the update of the
existing to-one target or collection item was silently lost.
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.64%. Comparing base (4f10194) to head (e5efef8).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8806   +/-   ##
=======================================
  Coverage   99.64%   99.64%           
=======================================
  Files         294      294           
  Lines       32934    32934           
  Branches     9573     9573           
=======================================
  Hits        32817    32817           
  Misses        113      113           
  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 merged commit d19dc8a into mikro-orm:master Nov 23, 2026
26 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