diff --git a/packages/orm/src/client/crud/operations/base.ts b/packages/orm/src/client/crud/operations/base.ts index d696f82ff..0314bcf95 100644 --- a/packages/orm/src/client/crud/operations/base.ts +++ b/packages/orm/src/client/crud/operations/base.ts @@ -1228,8 +1228,10 @@ export abstract class BaseOperationHandler { ); // only fields not consumed by base update will be used for this model finalData = baseUpdateResult.remainingFields; - // base update may change entity ids, update the filter - combinedWhere = baseUpdateResult.baseEntity; + // make sure to include only the id fields from the base entity in the final filter + combinedWhere = baseUpdateResult.baseEntity + ? getIdValues(this.schema, modelDef.baseModel!, baseUpdateResult.baseEntity) + : baseUpdateResult.baseEntity; // update this entity with fields in updated base if (baseUpdateResult.baseEntity) { diff --git a/tests/e2e/orm/plugin-infra/entity-mutation-hooks.test.ts b/tests/e2e/orm/plugin-infra/entity-mutation-hooks.test.ts index eb8a25f16..ceb063fe8 100644 --- a/tests/e2e/orm/plugin-infra/entity-mutation-hooks.test.ts +++ b/tests/e2e/orm/plugin-infra/entity-mutation-hooks.test.ts @@ -692,3 +692,45 @@ describe('Entity mutation hooks tests', () => { }); }); }); + +describe('Entity mutation hooks - delegate model interaction', () => { + it('update on child model succeeds with afterEntityMutation plugin', async () => { + console.log('RUNNING!!!!'); + const client = await createTestClient( + ` +model Base { + id Int @id @default(autoincrement()) + orgId Int + type String + @@delegate(type) +} + +model Child extends Base { + childField Int +} +`, + ); + + const withPlugin = client.$use({ + id: 'test', + onEntityMutation: { + afterEntityMutation() {}, + }, + }); + + const created = await withPlugin.child.create({ + data: { orgId: 1, childField: 10 }, + }); + + const updated = await withPlugin.child.update({ + where: { id: created.id }, + data: { orgId: 2, childField: 20 }, + }); + + expect(updated).toBeTruthy(); + expect(updated.orgId).toBe(2); + expect(updated.childField).toBe(20); + + await client.$disconnect(); + }); +});