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

Embeddables - undefined instead of null, smart query filter by null not allowed #4787

Closed
ml1nk opened this issue Oct 5, 2023 · 0 comments
Closed
Labels
bug Something isn't working

Comments

@ml1nk
Copy link
Contributor

ml1nk commented Oct 5, 2023

Describe the bug
If an embeddable is nullable and all columns of it are null, then the property is expected to be null but is currently undefined (forceUndefined is not set). In addition I expected { a: null } (see test case) to work, but got an exception.

Stack trace
await orm.em.findOne(B, { a: null })

TypeError: Cannot convert undefined or null to object
        at Function.keys (<anonymous>)

      65 |     }
      66 |
    > 67 |     const operator = Object.keys(payload[item]).some(f => Utils.isOperator(f));
         |                             ^
      68 |
      69 |     if (operator) {
      70 |       throw ValidationError.cannotUseOperatorsInsideEmbeddables(entityName, prop.name, payload);

      at Function.createObjectItemNode (packages/knex/src/query/CriteriaNodeFactory.ts:67:29)
      at packages/knex/src/query/CriteriaNodeFactory.ts:52:22
          at Array.reduce (<anonymous>)
      at Function.createObjectNode (packages/knex/src/query/CriteriaNodeFactory.ts:51:41)
      at Function.createNode (packages/knex/src/query/CriteriaNodeFactory.ts:22:19)
      at QueryBuilder.where (packages/knex/src/query/QueryBuilder.ts:278:46)
      at SqliteDriver.find (packages/knex/src/AbstractSqlDriver.ts:94:8)
      at SqliteDriver.findOne (packages/knex/src/AbstractSqlDriver.ts:136:28)
      at SqlEntityManager.findOne (packages/core/src/EntityManager.ts:535:34)
      at SqlEntityManager.findOneOrFail (packages/core/src/EntityManager.ts:580:16)
      at Object.<anonymous> (tests/issues/GHXXXX2.test.ts:54:15)

To Reproduce

import { Embeddable, Embedded, Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/sqlite';

@Embeddable()
export class A {

  @Property()
  test!: number;

}

@Entity()
export class B {

  @PrimaryKey()
  id!: number;

  @Embedded({ entity: () => A, object: false, nullable: true })
  a!: A  | null;

}

describe.only('GH issue XXXX', () => {

  let orm: MikroORM;

  beforeAll(async () => {
    orm = await MikroORM.init({
      entities: [A, B],
      dbName: ':memory:'
    });
    await orm.getSchemaGenerator().ensureDatabase();
    await orm.getSchemaGenerator().dropSchema();
    await orm.getSchemaGenerator().createSchema();

    orm.em.create(B, { a: null });
    await orm.em.flush();
    orm.em.clear();
  });

  afterAll(() => orm.close(true));

  test('embedded should be null', async () => {
    const f = await orm.em.getRepository(B).findAll()
    expect(f.length).toBe(1)
    expect(f[0].a).toBeNull(); // undefined
  });

  test('get embedded by null', async () => {

    const a = await orm.em.findOne(B, { a: { test: null } })
    expect(a).not.toBeNull()

    const f = await orm.em.findOne(B, { a: null }) // throws 
    expect(f).not.toBeNull()

  });
});

Versions

Dependency Version
mikro-orm cbc0c50
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