Repository navigation
Description
Issue description
When using MySQL bigint as PK/FK type, TypeORM incorrectly triggers cascading updates on child entities even when cascade is not enabled. This results in the foreign key of the child being set to NULL. Defining { nullable: false } on the ManyToOne relation produces a database error because the FK is overwritten with NULL. This behavior does not occur if PK/FK types are int.
Expected Behavior
- Saving a parent entity without
cascadeshould not modify child entities. - Foreign keys of child entities must remain intact unless explicitly changed.
bigintPK/FK should behave the same asintregarding cascading.
Actual Behavior
- Saving a parent entity triggers an unintended
cascadeto child entities. - The foreign key (
brandId) ofBrandPropertyis set toNULL. - Database error occurs if the relation is
nullable: false.
Steps to reproduce
Entity Definitions
@Entity()
class Brand {
@PrimaryGeneratedColumn({ type: "bigint" })
// @PrimaryGeneratedColumn({ type: "int" }) // Using int will not cascade update when saving the brand.
public id: string;
@OneToMany(
() => BrandProperty,
(property) => property.brand,
// intentionally no cascade here
)
public properties: BrandProperty[];
}
@Entity()
class BrandProperty {
@PrimaryGeneratedColumn({ type: "bigint" })
// @PrimaryGeneratedColumn({ type: "int" }) // Using int will not cascade update when saving the brand.
public id: string;
@ManyToOne(() => Brand, (brand) => brand.properties, {
// nullable: false, // Results in error when saving the brand, caused by the updated foreign key brandId of BrandProperty to NULL.
// nullable: true, // DEFAULT: Results in unintended NULL foreign key brandId when saving the brand.
})
public brand: Brand;
}Testing insert and save
Hint: orm = EntityManager
/**
* After the transaction is committed, the brand and brand property are inserted into the database correctly.
* Brand has id 1 and brand property has id 1 with foreign key brandId set to 1.
*
* Database after transaction is committed:
* Brand
* | id |
* | 1 |
*
* BrandProperty
* | id | brandId |
* | 1 | 1 |
*/
await this.orm.transaction(async (transaction) => {
const brand = new Brand();
await transaction.insert(Brand, brand);
const brandProperty = new BrandProperty();
brandProperty.brand = brand;
await transaction.insert(BrandProperty, brandProperty);
});
/**
* To prove that automated cascading will NULL the foreign key of the brand property we simply read the brand and save the brand.
* Expected result: Only brand should be affected by updated without cascading to the brand property.
* Actual result: Brand property is also updated (cascade) with foreign key brandId set to NULL.
*
* Database after transaction is committed:
* Brand
* | id |
* | 1 |
*
* BrandProperty
* | id | brandId |
* | 1 | NULL |
*
* Note: If we define { nullable: false } to our ManyToOne relation at BrandProperty->brand,
* the database will throw an error when trying to save the brand, caused by the updated foreign key brandId of BrandProperty.
*/
await this.orm.transaction(async (transaction) => {
const brand = await transaction.findOne(Brand, {
where: { id: Equal("1") },
relations: { properties: true },
});
// Save will trigger cascade and setting foreign key brandId of BrandProperty to NULL.
await transaction.save(Brand, brand);
});My Environment
| Dependency | Version |
|---|---|
| Operating System | Any (tested on macOS + Linux) |
| Node.js version | 18.20.8 |
| Typescript version | 5.8.3 |
| TypeORM version | 0.3.27 |
| mysql | 2.18.1 |
| Database | MySQL 8.0.21 Community Server |
Additional Context
Behavior occurs only with bigint PK/FK.
Changing to int fixes the automated cascade on save
Workaround: sequential save of parent and child in a transaction.
Very similar to: #11226
Issue persists in latest TypeORM release.
Sponsor context: https://opencollective.com/devowl
Relevant Database Driver(s)
- aurora-mysql
- aurora-postgres
- better-sqlite3
- cockroachdb
- cordova
- expo
- mongodb
- mysql
- nativescript
- oracle
- postgres
- react-native
- sap
- spanner
- sqlite
- sqlite-abstract
- sqljs
- sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, but I don't know how to start. I would need guidance.
Activity
G0maa commentedon Nov 22, 2025
Hello @iDschepe, thanks for reporting this issue.
I've tried to reproduce this locally on my machine, and I wasn't able to (
brandIdwas not set as null), here's a link to the repo where I tried to reproduce this bug.Thanks!
Arul1998 commentedon Jul 29, 2026
The FK-nulling comes from an id-comparison type mismatch, not from cascade configuration.
When the parent is saved, OneToManySubjectBuilder diffs the DB-loaded children against the in-memory children with OrmUtils.compareIds → compare2Objects. That comparison only applies its type-tolerant toString() check when both id values are the same primitive type (both string or both number); a mixed pair falls through and compares unequal. So when the same bigint id is a string ("1") on one side and a number (1) on the other, compareIds returns false, the DB child is treated as removed from the collection, and OneToManySubjectBuilder nullifies its FK (or deletes the row when the FK is non-nullable, which is the nullable: false error in the report).
Whether it triggers depends on the two sides ending up with different representations of the same bigint. MySQL returns bigint as a string by default, so it shows up there; with int both sides are numbers and it never fires which matches the report. That representation split is also driver/config-dependent (e.g. bigNumberStrings), which is likely why it doesn't reproduce on every setup. Looks like the same underlying cause as the closed #11226.
I'm happy to submit a PR. Before I do would you prefer (a) making entity-id comparison tolerant of the numeric-string vs number case, or (b) normalizing bigint to a consistent representation at the source? I can implement either; just want to match your intended direction.
fix(persistence): guard cross-type id comparison with Number.isSafeIn…
fix(persistence): keep decimal id coercion while rejecting unsafe int…
fix(persistence): compare mixed string/number ids by numeric value
fix: compare entity ids of mixed string/number type as equal
fix(persistence): guard cross-type id comparison with Number.isSafeIn…
fix(persistence): keep decimal id coercion while rejecting unsafe int…
fix(persistence): compare mixed string/number ids by numeric value