Repository navigation
Conversation
Code Review by Qodo
1.
|
Code Review by Qodo
1. Issue #11773 only unit-tested
|
|
This repository was recently restructured into a pnpm workspace, and the If your branch has diverged enough that a full rebase isn't worth it, Sorry for the disruption, and thanks for your patience. |
OrmUtils.compareIds only short-circuited when both ids shared the same primitive type (both string or both number). Drivers such as MySQL return bigint columns as strings, so a persisted id can be "1" on the DB-loaded side and 1 in memory. The mismatched pair matched no branch and fell through to a false result, making the relation look removed and nulling its FK on save. Compare single-id maps by their string form when the two types differ, so "1" still matches 1. Same-type comparisons are unchanged. Fixes typeorm#11773
Guard the string/number id path so NaN and Infinity are not coerced to match a "NaN"/"Infinity" string. Add unit tests for the non-finite and null id cases.
…teger Address review feedback on the mixed string/number entity-id comparison in OrmUtils.compareIds (typeorm#11773). Beyond Number.MAX_SAFE_INTEGER a JS number cannot represent a bigint exactly, so String(number) can round to and wrongly match a *different* bigint string. Replace the earlier Number.isFinite check with Number.isSafeInteger, which also excludes NaN/Infinity, so unreliable values decline to fast-match instead of risking a false positive. Tests: - add an unsafe-integer case (9007199254740993 must not match the bigint string "9007199254740992") and confirm MAX_SAFE_INTEGER still matches its string form - cover explicit null id-maps (the null-or-undefined test only exercised undefined) - add a functional regression test (test/github-issues/11773) proving a bigint child's FK is not nulled when a parent is re-saved with a numeric in-memory child id (mysql/mariadb)
Address the Qodo review nit: the null id-map assertions cast through `as any`. compareIds already handles null at runtime, so widen its parameter types to `ObjectLiteral | undefined | null` and drop the casts.
…egers Number.isSafeInteger also rejects finite decimals such as 1.5, so a decimal primary key loaded as "1.5" no longer matched the in-memory 1.5 and persistence diffing could treat the row as removed. Only reject integers beyond MAX_SAFE_INTEGER (which lose precision); finite non-integers stringify faithfully and keep matching their string form. Add a decimal-id test, and drop the redundant narrating comments from the typeorm#11773 functional test.
String coercion only matched decimals whose string form was canonical, so a scaled decimal key hydrated as "70.000" did not match the in-memory 70. Compare the mixed string/number single-id fast path by numeric value instead, so bigints returned as "1" and scaled decimals both match, while still rejecting non-finite values and unsafe integers beyond MAX_SAFE_INTEGER where a JS number cannot represent the id exactly. Give the typeorm#11773 fixture an int parent PK and a bigint child PK so the regression isolates the child orphan/FK-null path (a bigint parent could otherwise fail parent matching first).
Number() accepts permissive forms ("" -> 0, "0x10" -> 16, "1e2" -> 100),
which could coerce a distinct string key into matching a number. Require
the string side of a mixed string/number id comparison to be a plain
decimal numeral before comparing numerically.
3933180 to
acd9d0f
Compare
|
Thanks for the heads-up and the detailed migration notes, @michaelbromley no worries at all about the disruption. I've rebased fix/compare-ids-bigint-string onto the new packages/typeorm layout using the recommended raised-rename-limit rebase. The two modified files replayed automatically; the new issue-11773 test files (which rename detection can't map since they're additions) I moved into packages/typeorm/test/github-issues/11773/ to match the new structure. Branch is pushed and the diff is now entirely under packages/typeorm/. Verified locally: the package compiles cleanly and the OrmUtils unit suite passes (including the compareIds cases this PR adds). Ready for another look whenever you have a chance. |
Description of change
What this does: Makes
OrmUtils.compareIdstreat a single-idmap as equalwhen the two ids hold the same value but different primitive types — e.g.
"1"(string) and
1(number), including scaled decimals like"70.000"and70.Why it's needed:
compareIdsonly short-circuited when both ids shared thesame primitive type (both string or both number). Drivers such as MySQL/MariaDB
return
bigintcolumns as strings (bigNumberStringsdefaults to true), so apersisted id can be
"1"on the DB-loaded side and1in memory. That mismatchedpair matched no branch and fell through to
false(deepCompare→compare2Objectsalso only string-normalizes same-type primitives). As a result
OneToManySubjectBuildersaw the child as orphaned and, under the defaultorphanedRowAction: "nullify", nulled its FK on save — erroring when the FK isNOT NULL. This is whyintPKs are unaffected (both sides are numbers) butbigintPKs break (#11773).The change: In the single-
idfast path, when the two id types differ, compareby numeric value rather than string form, so
"1"matches1and a scaleddecimal
"70.000"matches70. Guards keep it safe:NaN/Infinitynever match.Number.MAX_SAFE_INTEGERnever match, so a rounded JS numbercan't collide with a different
bigintstring.Number()forms(
""→ 0,"0x10"→ 16,"1e2"→ 100) can't coerce distinct keys together.Same-type comparisons are unchanged (still
firstId.id === secondId.id), and thegeneral-purpose
compare2Objectsis left untouched to avoid affecting jsonb/objectdiffing elsewhere.
How it was verified:
test/unit/util/orm-utils.test.ts) forcompareIds: same-typeequality, string-vs-number equality (
"1"===1), decimals incl. scale(
"70.000"===70), unequal string/number stayingfalse,null/undefinedhandling,
NaN/Infinityrejection, unsafe-integer rejection, permissivenumeric-string rejection, and the composite-id
deepComparefallback — allpassing.
test/github-issues/11773, mysql/mariadb) with anintparent PK and abigintchild PK. On re-save the parent always matches, sothe test can only fail the way Automated cascade sets FK to NULL for bigint relations when saving parent entity (MySQL) #11773 actually failed — the child's
bigintid is1in memory and"1"from the driver — and asserts the child's FK survives.Notes for maintainers:
idprimary keys still route throughdeepCompareand are out ofscope for this narrow, single-column fix.
(e.g.
"0.10000000000000001"vs0.1); that is IEEE-754Number()behavior,unrealistic for a real
bigint/int/fixed-scaledecimalprimary key, and out ofscope here. Happy to take a driver-level normalization direction instead if you'd
prefer solving the whole coercion class.
Pull-Request Checklist
masterbranchFixes #11773(
test/unit/util/orm-utils.test.ts,test/github-issues/11773)docs/docs/**.md) — N/A (internal comparison fix, no public API/doc change)