@mikro-orm/core 7.0.0-dev.167 → 7.0.0-dev.168

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -454,6 +454,16 @@ export class MetadataDiscovery {
454
454
  if (!prop.referencedColumnNames) {
455
455
  prop.referencedColumnNames = fieldNames;
456
456
  }
457
+ // Relations to composite PK targets need cascade update by default,
458
+ // since composite PKs are more likely to have mutable components
459
+ if (meta2.compositePK) {
460
+ prop.updateRule ??= 'cascade';
461
+ }
462
+ // Nullable relations default to 'set null' on delete - when the referenced
463
+ // entity is deleted, set the FK to null rather than failing
464
+ if (prop.nullable) {
465
+ prop.deleteRule ??= 'set null';
466
+ }
457
467
  }
458
468
  initOneToManyFields(prop) {
459
469
  const meta2 = prop.targetMeta;
@@ -469,9 +479,14 @@ export class MetadataDiscovery {
469
479
  const pks = Object.values(meta.properties).filter(prop => prop.primary);
470
480
  meta.primaryKeys = pks.map(prop => prop.name);
471
481
  meta.compositePK = pks.length > 1;
472
- // FK used as PK, we need to cascade
473
- if (pks.length === 1 && pks[0].kind !== ReferenceKind.SCALAR) {
474
- pks[0].deleteRule ??= 'cascade';
482
+ // FK used as PK, we need to cascade - applies to both single FK-as-PK
483
+ // and composite PKs where all PKs are FKs (e.g., pivot-like entities)
484
+ const fkPks = pks.filter(pk => pk.kind !== ReferenceKind.SCALAR);
485
+ if (fkPks.length > 0 && fkPks.length === pks.length) {
486
+ for (const pk of fkPks) {
487
+ pk.deleteRule ??= 'cascade';
488
+ pk.updateRule ??= 'cascade';
489
+ }
475
490
  }
476
491
  meta.forceConstructor ??= this.shouldForceConstructorUsage(meta);
477
492
  this.validator.validateEntityDefinition(this.metadata, meta.class, this.config.get('discovery'));
@@ -655,10 +670,9 @@ export class MetadataDiscovery {
655
670
  deleteRule: prop.deleteRule,
656
671
  createForeignKeyConstraint: prop.createForeignKeyConstraint,
657
672
  };
658
- if (selfReferencing && !this.platform.supportsMultipleCascadePaths()) {
659
- ret.updateRule ??= 'no action';
660
- ret.deleteRule ??= 'no action';
661
- }
673
+ const defaultRule = selfReferencing && !this.platform.supportsMultipleCascadePaths() ? 'no action' : 'cascade';
674
+ ret.updateRule ??= defaultRule;
675
+ ret.deleteRule ??= defaultRule;
662
676
  const meta = this.metadata.get(type);
663
677
  ret.targetMeta = meta;
664
678
  ret.joinColumns = [];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.167",
4
+ "version": "7.0.0-dev.168",
5
5
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -844,6 +844,16 @@ export interface Options<Driver extends IDatabaseDriver = IDatabaseDriver, EM ex
844
844
  * Database name to use for management operations (e.g., creating/dropping databases).
845
845
  */
846
846
  managementDbName?: string;
847
+ /**
848
+ * Default ON UPDATE rule for foreign keys.
849
+ * When not set, no rule is emitted and the database uses its native default (NO ACTION/RESTRICT).
850
+ */
851
+ defaultUpdateRule?: 'cascade' | 'no action' | 'set null' | 'set default' | 'restrict';
852
+ /**
853
+ * Default ON DELETE rule for foreign keys.
854
+ * When not set, no rule is emitted and the database uses its native default (NO ACTION/RESTRICT).
855
+ */
856
+ defaultDeleteRule?: 'cascade' | 'no action' | 'set null' | 'set default' | 'restrict';
847
857
  };
848
858
  /**
849
859
  * Embeddable entity configuration options.
package/utils/Utils.js CHANGED
@@ -123,7 +123,7 @@ export function parseJsonSafe(value) {
123
123
  }
124
124
  export class Utils {
125
125
  static PK_SEPARATOR = '~~~';
126
- static #ORM_VERSION = '7.0.0-dev.167';
126
+ static #ORM_VERSION = '7.0.0-dev.168';
127
127
  /**
128
128
  * Checks if the argument is instance of `Object`. Returns false for arrays.
129
129
  */