You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`=NULLwhere`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()exportclassA{
@Property()alpha!: number|null;
@ManyToOne(()=>C,{onDelete: 'CASCADE',onUpdateIntegrity: 'cascade'})c!: Ref<C>}
@Entity()exportclassB{
@PrimaryKey()id!: number;
@Embedded({entity: ()=>A,object: false,nullable: true})a!: A|null;}
@Entity()exportclassC{[PrimaryKeyType]!: number[PrimaryKeyProp]!: 'node'
@OneToOne({entity: ()=>D,primary: true,onDelete: 'cascade',onUpdateIntegrity: 'cascade'})node!: Ref<D>}
@Entity()exportclassD{
@PrimaryKey()id!: number;}
@Embeddable()exportclassA2{
@Property()alpha!: number|null;
@ManyToOne(()=>C2,{onDelete: 'CASCADE',onUpdateIntegrity: 'cascade'})c2!: Ref<C2>}
@Entity()exportclassB2{
@PrimaryKey()id!: number;
@Embedded({entity: ()=>A2,object: false,nullable: true})a2!: A2|null;}
@Entity()exportclassC2{
@PrimaryKey()id!: number;}exportclassFooBarSubscriberimplementsEventSubscriber{asyncafterUpdate(args: EventArgs<any>): Promise<void>{thrownewError("afterUpdate called but nothing changed");}}describe.only('GH issue ',()=>{letorm: MikroORM;beforeAll(async()=>{orm=awaitMikroORM.init({entities: [A,B,C,D,A2,B2,C2],dbName: ':memory:',debug: true,subscribers:[newFooBarSubscriber()]});awaitorm.getSchemaGenerator().ensureDatabase();awaitorm.getSchemaGenerator().dropSchema();awaitorm.getSchemaGenerator().createSchema();});afterAll(()=>orm.close(true));test('embedded with ManyToOne to foreign key as primary key entity',async()=>{orm.em.create(B,{a: null});awaitorm.em.flush();orm.em.clear();awaitorm.em.getRepository(B).findAll()awaitorm.em.flush();// update query is createdorm.em.clear()});test('embedded with ManyToOne to entity',async()=>{orm.em.create(B2,{a2: null});awaitorm.em.flush();orm.em.clear();awaitorm.em.getRepository(B2).findAll()awaitorm.em.flush();// no update query is createdorm.em.clear()});});
Additional context
Trying to reproduce the behavior lead to #4787
The text was updated successfully, but these errors were encountered:
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
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:
To Reproduce
Additional context
Trying to reproduce the behavior lead to #4787
Versions
The text was updated successfully, but these errors were encountered: