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 - update query gets executed even without change in some cases #4788

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 there is an ManyToOne relation inside a embeddable and the related entity uses foreign key as a primary key and the embeddable is nullable, then it generates an update on flush without even accessing any of the properties of the entity.

The faulty testcase produces the following sql:

update `b` set `a_c_node_id` = NULL where `id` = 1

To Reproduce

import { Embeddable, Embedded, Entity, EventArgs, EventSubscriber, ManyToOne, OneToOne, PrimaryKey, PrimaryKeyProp, PrimaryKeyType, Property, Ref } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/sqlite';

@Embeddable()
export class A {

  @Property()
  alpha!: number | null;

  @ManyToOne(() => C, { onDelete: 'CASCADE', onUpdateIntegrity: 'cascade' })
  c!: Ref<C>

}

@Entity()
export class B {

  @PrimaryKey()
  id!: number;

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


}

@Entity()
export class C {

  [PrimaryKeyType]!: number
  [PrimaryKeyProp]!: 'node'
  @OneToOne({ entity: ()=> D, primary: true, onDelete: 'cascade', onUpdateIntegrity: 'cascade' })
    node!: Ref<D>

}


@Entity()
export class D {
  @PrimaryKey()
  id!: number;
}




@Embeddable()
export class A2 {

  @Property()
  alpha!: number | null;

  @ManyToOne(() => C2, { onDelete: 'CASCADE', onUpdateIntegrity: 'cascade' })
  c2!: Ref<C2>

}

@Entity()
export class B2 {

  @PrimaryKey()
  id!: number;

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


}

@Entity()
export class C2 {

  @PrimaryKey()
  id!: number;

}


export class FooBarSubscriber implements EventSubscriber {
  async afterUpdate(args: EventArgs<any>): Promise<void> {
    throw new Error("afterUpdate called but nothing changed");
  }
}

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

  let orm: MikroORM;

  beforeAll(async () => {
    orm = await MikroORM.init({
      entities: [A, B, C, D, A2, B2, C2],
      dbName: ':memory:',
      debug: true,
      subscribers:[new FooBarSubscriber()]
    });
    await orm.getSchemaGenerator().ensureDatabase();
    await orm.getSchemaGenerator().dropSchema();
    await orm.getSchemaGenerator().createSchema();
  });

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


  test('embedded with ManyToOne to foreign key as primary key entity', async () => {
    orm.em.create(B, { a: null });
    await orm.em.flush();
    orm.em.clear();
    await orm.em.getRepository(B).findAll()
    await orm.em.flush(); // update query is created
    orm.em.clear()
  });


  test('embedded with ManyToOne to entity', async () => {
    orm.em.create(B2, { a2: null });
    await orm.em.flush();
    orm.em.clear();
    await orm.em.getRepository(B2).findAll()
    await orm.em.flush(); // no update query is created
    orm.em.clear()
  });
});

Additional context
Trying to reproduce the behavior lead to #4787

Versions

Dependency Version
mikro-orm cbc0c50
@ml1nk ml1nk changed the title Embeddables - update query without change in some cases Embeddables - update query gets executed even without change in some cases Oct 5, 2023
@B4nan B4nan added the bug Something isn't working label Oct 5, 2023
@B4nan B4nan closed this as completed in 77ffa4f Oct 5, 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