Skip to content

Commit aaab890

Browse files
committed
fix: enhance delegate model interaction with afterEntityMutation plugin support in child updates
1 parent 2564cd4 commit aaab890

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

packages/orm/src/client/crud/operations/base.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,8 +1218,10 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
12181218
);
12191219
// only fields not consumed by base update will be used for this model
12201220
finalData = baseUpdateResult.remainingFields;
1221-
// base update may change entity ids, update the filter
1222-
combinedWhere = baseUpdateResult.baseEntity;
1221+
// trim to id fields only to avoid "missing FROM-clause" errors in the child update
1222+
combinedWhere = baseUpdateResult.baseEntity
1223+
? getIdValues(this.schema, modelDef.baseModel!, baseUpdateResult.baseEntity)
1224+
: baseUpdateResult.baseEntity;
12231225

12241226
// update this entity with fields in updated base
12251227
if (baseUpdateResult.baseEntity) {

tests/e2e/orm/plugin-infra/entity-mutation-hooks.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,4 +691,45 @@ describe('Entity mutation hooks tests', () => {
691691
await expect(client.user.findMany()).toResolveWithLength(0);
692692
});
693693
});
694+
695+
describe('Delegate model interaction', () => {
696+
it('update on child model succeeds with afterEntityMutation plugin', async () => {
697+
const client = await createTestClient(
698+
`
699+
model Base {
700+
id Int @id @default(autoincrement())
701+
orgId Int
702+
type String
703+
@@delegate(type)
704+
}
705+
706+
model Child extends Base {
707+
childField Int
708+
}
709+
`,
710+
);
711+
712+
const withPlugin = client.$use({
713+
id: 'test',
714+
onEntityMutation: {
715+
afterEntityMutation() {},
716+
},
717+
});
718+
719+
const created = await withPlugin.child.create({
720+
data: { orgId: 1, childField: 10 },
721+
});
722+
723+
const updated = await withPlugin.child.update({
724+
where: { id: created.id },
725+
data: { orgId: 2, childField: 20 },
726+
});
727+
728+
expect(updated).toBeTruthy();
729+
expect(updated.orgId).toBe(2);
730+
expect(updated.childField).toBe(20);
731+
732+
await client.$disconnect();
733+
});
734+
});
694735
});

0 commit comments

Comments
 (0)