Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[BUG] STI subclass @OneToMany override crashes metadata discovery #7635

Copy link
Copy link
@belmeopmenieuwesim

Description

@belmeopmenieuwesim
Issue body actions

When two STI subclasses both declare a @OneToMany with the same property name but different target entities, MikroORM.init crashes:

TypeError: rootProp.fieldNames is not iterable
    at MetadataDiscovery.js:1339:49
    at MetadataDiscovery.initSingleTableInheritance (MetadataDiscovery.js:1311)

Looks like the inverse-side counterpart of #7598. The fix in #7599 added sameRelationTargetRoot() to skip the rename branch for narrowed owning-side overrides, but it bails out for anything that isn't MANY_TO_ONE or ONE_TO_ONE:

if (prop.kind !== ReferenceKind.MANY_TO_ONE && prop.kind !== ReferenceKind.ONE_TO_ONE) {
  return false;
}

So OneToMany falls through into the rename branch, hits initFieldName(rootProp, ...) which doesn't set fieldNames for inverse-side props, and then [...rootProp.fieldNames] blows up.

The targets in the repro aren't narrowing the same hierarchy either — they're entirely separate entities — so the rename branch shouldn't run for them at all.

Expected behavior

Discovery should succeed. OneToMany on STI siblings has no column to merge, so it should just inherit.

Reproduction

npm i @mikro-orm/core@7.0.13 @mikro-orm/postgresql@7.0.13 and run:

import { MikroORM, defineEntity } from '@mikro-orm/core';

const p = defineEntity.properties;

const Animal = defineEntity({
  name: 'Animal',
  abstract: true,
  discriminatorColumn: 'type',
  properties: {
    id: p.string().primary(),
    type: p.string(),
    name: p.string(),
  },
});

const DogToy = defineEntity({
  name: 'DogToy',
  properties: {
    id: p.string().primary(),
    name: p.string(),
    owner: () => p.manyToOne(Dog),
  },
});

const CatToy = defineEntity({
  name: 'CatToy',
  properties: {
    id: p.string().primary(),
    name: p.string(),
    owner: () => p.manyToOne(Cat),
  },
});

const Dog = defineEntity({
  name: 'Dog',
  extends: Animal,
  discriminatorValue: 'DOG',
  properties: {
    toys: () => p.oneToMany(DogToy).mappedBy('owner'),
  },
});

const Cat = defineEntity({
  name: 'Cat',
  extends: Animal,
  discriminatorValue: 'CAT',
  properties: {
    toys: () => p.oneToMany(CatToy).mappedBy('owner'),
  },
});

const { PostgreSqlDriver } = await import('@mikro-orm/postgresql');
await MikroORM.init({
  driver: PostgreSqlDriver,
  entities: [Animal, Dog, Cat, DogToy, CatToy],
  dbName: 'never_connects',
  connect: false,
});

What driver are you using?

@mikro-orm/postgresql

MikroORM version

7.0.13

Node.js version

24.15.0

Operating system

Windows 11

Validations

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.