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

Find query on entity with null property value triggers an update query after flushing when forceUndefined is set to true #4412

Closed
JustLitio opened this issue Jun 1, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@JustLitio
Copy link

JustLitio commented Jun 1, 2023

Previous issues related to this one

#4262
#4266

Describe the bug

If I retrieve Entity A and a nullable property has a null value, an update query on Entity A gets triggered after flushing. Seemlingly, this is because of using forceUndefined: true, and the nullable property gets converted to undefined on entity retrieval, and that gets registered as a change that has to be persisted.

This was noted as fixed in v5.7.2, but now it's happening again

To reproduce

import {
  Entity,
  PrimaryKey,
  Property,
  SimpleLogger,
} from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/postgresql';
import { mockLogger } from '../../helpers';

@Entity()
export class A {

  @PrimaryKey({ type: 'number' })
  id!: number;

  @Property({
    length: 3,
    name: 'created_at',
    nullable: true,
    type: 'datetime',
  })
  createdAt?: Date;

  @Property({ length: 64, name: 'name', type: 'varchar' })
  name!: string;

}

describe('GH issue 4412', () => {
  let orm: MikroORM;

  beforeAll(async () => {
    orm = await MikroORM.init({
      dbName: ':memory:',
      entities: [A],
      forceUndefined: true,
      loggerFactory: options => new SimpleLogger(options),
    });

    await orm.schema.refreshDatabase();
  });

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

  test('update triggered after findOne with forceUndefined true', async () => {
    const mock = mockLogger(orm);
    let a: A | null = orm.em.create(A, {
      id: 1,
      name: 'a',
    });

    await orm.em.persistAndFlush(a);

    a = await orm.em.findOne(A, { name: 'a' });

    await orm.em.flush();

    expect(mock.mock.calls).toEqual([
      ['[query] begin'],
      ["[query] insert into \"a\" (\"id\", \"name\") values (1, 'a') returning \"id\""],
      ['[query] commit'],
      ["[query] select \"a0\".* from \"a\" as \"a0\" where \"a0\".\"name\" = 'a' limit 1"],
      ['[query] begin'],
      ['[query] update "a" set "created_at" = NULL where "id" = 1'],
      ['[query] commit'],
    ]);
  });
});

Versions

Dependency Version
node v18.16.0
typescript 5.0.4
mikro-orm 5.7.10
driver postgresql
@B4nan B4nan added the bug Something isn't working label Jun 1, 2023
@B4nan B4nan closed this as completed in 9303c3f Jun 1, 2023
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