Skip to content

Commit

Permalink
fix(delegate): push base-level id assignment to base payload when cre…
Browse files Browse the repository at this point in the history
…ating a concrete entity
  • Loading branch information
ymc9 committed Jun 19, 2024
1 parent 908048b commit 50cc786
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/runtime/src/enhancements/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}

// ensure the full nested "create" structure is created for base types
private ensureBaseCreateHierarchy(model: string, result: any) {
let curr = result;
private ensureBaseCreateHierarchy(model: string, args: any) {
let curr = args;
let base = this.getBaseModel(model);
let sub = this.getModelInfo(model);

Expand All @@ -478,6 +478,16 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
curr[baseRelationName].create[base.discriminator] = sub.name;
}
}

// Look for base id field assignments in the current level, and push
// them down to the base level
for (const idField of getIdFields(this.options.modelMeta, base.name)) {
if (curr[idField.name] !== undefined) {
curr[baseRelationName].create[idField.name] = curr[idField.name];
delete curr[idField.name];
}
}

curr = curr[baseRelationName].create;
sub = base;
base = this.getBaseModel(base.name);
Expand Down
31 changes: 31 additions & 0 deletions tests/regression/tests/issue-1518.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { loadSchema } from '@zenstackhq/testtools';
describe('issue 1518', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model Activity {
id String @id @default(uuid())
title String
type String
@@delegate(type)
@@allow('all', true)
}
model TaskActivity extends Activity {
description String
@@map("task_activity")
@@allow('all', true)
}
`
);

const db = enhance();
await db.taskActivity.create({
data: {
id: '00000000-0000-0000-0000-111111111111',
title: 'Test Activity',
description: 'Description of task',
},
});
});
});

0 comments on commit 50cc786

Please sign in to comment.