Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/orm/src/client/crud/operations/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1228,8 +1228,10 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
);
// 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) {
Expand Down
42 changes: 42 additions & 0 deletions tests/e2e/orm/plugin-infra/entity-mutation-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Loading