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

getQuery causing inconsistencies in queryBuilder execution with JsonType and complex query #5247

Closed
5 tasks done
Darent98 opened this issue Feb 16, 2024 · 0 comments
Closed
5 tasks done
Labels
bug Something isn't working

Comments

@Darent98
Copy link

Darent98 commented Feb 16, 2024

Describe the bug

I've identified a bug related to an entity containing a property of type JsonType. This bug occurs specifically when executing a query builder (qb) with a complex where statement after invoking the getQuery() method on the qb object.

Interestingly, the complex query functions correctly when the getQuery() method is not called before execution. Additionally, I've provided a simple query where the bug is not reproducible.

To reproduce the error, you can utilize the provided reproduction case.

A workaround for this issue is to use qb.clone(true).getQuery(). However, it's worth noting that the observed behavior with both simple and complex queries appears inconsistent.

Reproduction

import { Entity, JsonType, MikroORM, PrimaryKey, Property, QBFilterQuery } from '@mikro-orm/sqlite';

@Entity()
class Test {

  @PrimaryKey()
  id!: number;

  @Property({type: JsonType, nullable: true})
  a!: any

  constructor(a: any) {
    this.a = a
  }

}

let orm: MikroORM;

beforeAll(async () => {
  orm = await MikroORM.init({
    dbName: ':memory:',
    entities: [Test],
    debug: ['query', 'query-params'],
    allowGlobalContext: true, // only for testing
  });
  await orm.schema.refreshDatabase();
  orm.em.create(Test, { a: {
    value: 1
  } });
  orm.em.create(Test, { a: {
    complex: {
      bool: true
    }
  } });
  await orm.em.flush();
  orm.em.clear();
});

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

test('simple - no getQuery', async () => {
  const query: QBFilterQuery<Test> = {a: {value: 1}}
  const qb = orm.em.qb(Test).andWhere(query)
  const res = await qb.execute() // proper sql query is generated
  expect(res.length).toBe(1) // result as expected
})

test('simple - with getQuery', async () => {
  const query: QBFilterQuery<Test> = {a: {value: 1}}
  const qb = orm.em.qb(Test).andWhere(query)
  qb.getQuery()
  const res = await qb.execute() // proper sql query is generated
  expect(res.length).toBe(1) // result as expected
})

test('complex working', async () => {
  const query = {$and: [{$or: [{a: {value: 1}}, {a: {complex: {bool: true}}}]}]}
  const qb = orm.em.qb(Test).andWhere(query)
  const res = await qb.execute() // proper sql query is generated
  expect(res.length).toBe(2); // result as expected
});

test('complex not working', async () => {
  const query = {$and: [{$or: [{a: {value: 1}}, {a: {complex: {bool: true}}}]}]}
  const qb = orm.em.qb(Test).andWhere(query)
  qb.getQuery()
  const res = await qb.execute() // faulty sql query is generated
  expect(res.length).toBe(2)
});

What driver are you using?

@mikro-orm/sqlite

MikroORM version

6.1.1-dev.3

Node.js version

v20.10.0

Operating system

Linux

Validations

@B4nan B4nan added the bug Something isn't working label Feb 16, 2024
@B4nan B4nan closed this as completed in f79a752 Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants