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

Incorrect migration when using schema #4918

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

Incorrect migration when using schema #4918

bakura10 opened this issue Nov 11, 2023 · 0 comments

Comments

@bakura10
Copy link

Hi !

When using a schema on a table A, but not on table B, an incorrect SQL is generated for migrations.

To Reproduce

Entity one.ts (with a schema):

import { Entity, PrimaryKey, ManyToOne, type Rel } from "@mikro-orm/postgresql";
import { Two } from "./two.js";

@Entity({ tableName: 'one', schema: 'test' })
export class One {
  @PrimaryKey()
  id: string = crypto.randomUUID();

  @ManyToOne(() => Two, { deleteRule: 'cascade' })
  two!: Rel<Two>;
}

Entity two.ts (without schema):

import { Entity, PrimaryKey } from "@mikro-orm/postgresql";

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

Generated migration:

import { Migration } from '@mikro-orm/migrations';

export class Migration20231111090839 extends Migration {

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

    this.addSql('create table "two" ("id" varchar(255) not null, constraint "two_pkey" primary key ("id"));');

    this.addSql('create table "test"."one" ("id" varchar(255) not null, "two_id" varchar(255) not null, constraint "one_pkey" primary key ("id"));');

    this.addSql('alter table "test"."one" add constraint "one_two_id_foreign" foreign key ("two_id") references "test"."two" ("id") on update cascade on delete cascade;');
  }

  async down(): Promise<void> {
    this.addSql('drop table if exists "two" cascade;');

    this.addSql('drop table if exists "test"."one" cascade;');

    this.addSql('drop schema "test";');
  }

}

It tries to create a foreign key referencing "test.two", which does not exist (two does not have any schema).

Versions

| node | 20 |
| mikro-orm | 6.0-rc0 |
| 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