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

Multi-schema relation queries not respecting entity schema's #2740

Closed
jeroenverfallie opened this issue Feb 10, 2022 · 0 comments
Closed

Multi-schema relation queries not respecting entity schema's #2740

jeroenverfallie opened this issue Feb 10, 2022 · 0 comments

Comments

@jeroenverfallie
Copy link

jeroenverfallie commented Feb 10, 2022

Given a multi-schema setup, the QueryBuilder is not respecting the defined schema when querying on a related entity.

Steps to reproduce the behavior:

I'm not sure how to add a test like this, so:

// tests/issues/GH2740.test.ts
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property, MikroORM } from '@mikro-orm/core';
import type { PostgreSqlDriver } from '@mikro-orm/postgresql';

@Entity({
  tableName: 'person',
  schema: 'foo',
})
export class PersonEntity {

  @PrimaryKey()
  id!: number;

  @Property()
  name!: string;

}

@Entity({
  tableName: 'task',
  schema: 'bar',
})
export class TaskEntity {

  @PrimaryKey()
  id!: number;

  @ManyToOne(() => PersonEntity)
  person!: PersonEntity;

}

describe('GH #2740', () => {

  let orm: MikroORM<PostgreSqlDriver>;

  beforeAll(async () => {
    orm = await MikroORM.init({
      type: 'postgresql',
      dbName: 'mikro_orm_test_gh_2740',
      entities: [PersonEntity, TaskEntity],
    });
    await orm.getSchemaGenerator().ensureDatabase();
    await orm.getSchemaGenerator().dropSchema();
    await orm.getSchemaGenerator().createSchema();
  });

  afterAll(async () => {
    await orm.close();
  });

  it('should respect the defined schema in queries on relations', async () => {
    const qb = orm.em.createQueryBuilder(TaskEntity).where({
      person: { name: 'test' },
    });

    expect(qb.getQuery()).toBe(`select "t0".* from "bar"."task" as "t0" left join "foo"."person" as "p1" on "t0"."person_id" = "p1"."id" where "p1"."name" = $1`);
  });

});
$ jest --runInBand tests/issues/GH2740.test.ts
 FAIL  tests/issues/GH2740.test.ts
  GH #2740
    ✕ should respect the defined schema in queries on relations (9 ms)

  ● GH #2740 › should respect the defined schema in queries on relations

    expect(received).toBe(expected) // Object.is equality

    Expected: "select \"t0\".* from \"bar\".\"task\" as \"t0\" left join \"foo\".\"person\" as \"p1\" on \"t0\".\"person_id\" = \"p1\".\"id\" where \"p1\".\"name\" = $1"
    Received: "select \"t0\".* from \"bar\".\"task\" as \"t0\" left join \"bar\".\"person\" as \"p1\" on \"t0\".\"person_id\" = \"p1\".\"id\" where \"p1\".\"name\" = $1"

      54 |     });
      55 |
    > 56 |     expect(qb.getQuery()).toBe(`select "t0".* from "bar"."task" as "t0" left join "foo"."person" as "p1" on "t0"."person_id" = "p1"."id" where "p1"."name" = $1`);
         |                           ^
      57 |   });
      58 |
      59 | });

      at Object.<anonymous> (tests/issues/GH2740.test.ts:56:27)

Versions

Dependency Version
@mikro-orm/core 5.0.0
@mikro-orm/postgresql 5.0.0
@B4nan B4nan closed this as completed in 717aa5e Feb 11, 2022
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