Skip to content

Issues with bidirectionalRelations Generation in Entity Models #5738

@Ian-Eduware

Description

@Ian-Eduware

Describe the bug

I am encountering an issue with Mikro-ORM where the entity generator does not automatically create the relationship properties between Users and Licenses. Although I have defined the relationship in the database schema, the entities generated do not reflect this relationship as expected

Reproduction

import { MsSqlDriver, Options } from '@mikro-orm/mssql';
import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
import dotenv from 'dotenv';
dotenv.config();

const config: Options = {
    driver: MsSqlDriver,
    host: process.env.DB_HOST,
    port: 1433,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    dbName: process.env.DB_NAME,
    entities: ['dist/entities/**/*.js'],
    entitiesTs: ['src/entities/**/*.ts'],
    discovery: {
        warnWhenNoEntities: false,
    },
    metadataProvider: TsMorphMetadataProvider,
    baseDir: process.cwd(),
    debug: true,
};
export default config;
// scripts/generate-entities.ts
import { MikroORM } from '@mikro-orm/core';
import { EntityGenerator } from '@mikro-orm/entity-generator';
import config from '../src/config/mikro-orm.config';

const generateEntities = async () => {
    const orm = await MikroORM.init({
        ...config,
        discovery: { warnWhenNoEntities: false },
        extensions: [EntityGenerator],
    });
    const dump = await orm.entityGenerator.generate({
        save: true,
        path: process.cwd() + '/src/entities',
        bidirectionalRelations: true,
    });
    await orm.close(true);
};

generateEntities().catch((err) => {
    console.error(err);
    process.exit(1);
});
@Entity({ tableName: 'Users' })
export class Users {
  @PrimaryKey({ fieldName: 'UserID' })
  UserID!: number;

  @Property({ fieldName: 'Username', length: 255 })
  Username!: string;

}

@Entity({ tableName: 'Licenses' })
export class Licenses {
  @PrimaryKey({ fieldName: 'LicenseID' })
  LicenseID!: number;

 @ManyToOne({ entity: () => Users, fieldName: 'UserID', index: '<Index_License_UserID>' })
  UserID!: Users;

}

What driver are you using?

@mikro-orm/mssql

MikroORM version

^6.2.9

Node.js version

v22.3.0

Operating system

Windows 11

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions