Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema not working with single table inheritance #4933

Closed
bakura10 opened this issue Nov 17, 2023 · 0 comments
Closed

Schema not working with single table inheritance #4933

bakura10 opened this issue Nov 17, 2023 · 0 comments

Comments

@bakura10
Copy link

Describe the bug
Hi,

When using the single table inheritance with a schema on the base table, incorrect relations are created.

To Reproduce

Here is the entities:

import { Entity, PrimaryKey, ManyToOne, Enum, type Rel } from "@mikro-orm/postgresql";

@Entity({ tableName: 'users', schema: 'example', discriminatorColumn: 'type', abstract: true })
export class Base {
  @PrimaryKey()
  id: string = crypto.randomUUID();

  @Enum()
  type!: 'one' | 'two';
}

@Entity({ discriminatorValue: 'one' })
export class One extends Base {
}

@Entity({ discriminatorValue: 'two' })
export class Two extends Base {
}

@Entity({ tableName: 'relations' })
export class Relation {
  @PrimaryKey()
  id: string = crypto.randomUUID();

  @ManyToOne(() => Two, { deleteRule: 'set null' })
  appliedBy?: Rel<Two>;
}

Here is the migration generated:

async up(): Promise<void> {
    this.addSql('create schema if not exists "example";');

    this.addSql('create table "example"."users" ("id" varchar(255) not null, "type" text check ("type" in (\'one\', \'two\')) not null, constraint "users_pkey" primary key ("id"));');
    this.addSql('create index "users_type_index" on "example"."users" ("type");');

    this.addSql('create table "relations" ("id" varchar(255) not null, "applied_by_id" varchar(255) not null, constraint "relations_pkey" primary key ("id"));');

    this.addSql('alter table "relations" add constraint "relations_applied_by_id_foreign" foreign key ("applied_by_id") references "users" ("id") on update cascade on delete set null;');
  }

Expected behavior
The foreign key "applied_by_id" references the "users" table without the schema.

Versions

| Dependency | Version |
| node | 20 |
| mikro-orm | 6.0.0-dev176 |
| your-driver | postgresql |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant