Skip to content

Commit a14bf29

Browse files
authored
fix(delegate): push base-level id assignment to base payload when creating a concrete entity (#1521)
1 parent 908048b commit a14bf29

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

packages/runtime/src/enhancements/delegate.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
460460
}
461461

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

@@ -478,6 +478,16 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
478478
curr[baseRelationName].create[base.discriminator] = sub.name;
479479
}
480480
}
481+
482+
// Look for base id field assignments in the current level, and push
483+
// them down to the base level
484+
for (const idField of getIdFields(this.options.modelMeta, base.name)) {
485+
if (curr[idField.name] !== undefined) {
486+
curr[baseRelationName].create[idField.name] = curr[idField.name];
487+
delete curr[idField.name];
488+
}
489+
}
490+
481491
curr = curr[baseRelationName].create;
482492
sub = base;
483493
base = this.getBaseModel(base.name);

tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,22 @@ describe('Polymorphism Test', () => {
158158
).resolves.toMatchObject({ count: 2 });
159159
});
160160

161+
it('create concrete with explicit id', async () => {
162+
const { enhance } = await loadSchema(schema, { enhancements: ['delegate'] });
163+
const db = enhance();
164+
165+
await expect(
166+
db.ratedVideo.create({ data: { id: 1, duration: 100, url: 'xyz', rating: 5 } })
167+
).resolves.toMatchObject({
168+
id: 1,
169+
duration: 100,
170+
url: 'xyz',
171+
rating: 5,
172+
assetType: 'Video',
173+
videoType: 'RatedVideo',
174+
});
175+
});
176+
161177
it('read with concrete', async () => {
162178
const { db, user, video } = await setup();
163179

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
describe('issue 1518', () => {
3+
it('regression', async () => {
4+
const { enhance } = await loadSchema(
5+
`
6+
model Activity {
7+
id String @id @default(uuid())
8+
title String
9+
type String
10+
@@delegate(type)
11+
@@allow('all', true)
12+
}
13+
14+
model TaskActivity extends Activity {
15+
description String
16+
@@map("task_activity")
17+
@@allow('all', true)
18+
}
19+
`
20+
);
21+
22+
const db = enhance();
23+
await db.taskActivity.create({
24+
data: {
25+
id: '00000000-0000-0000-0000-111111111111',
26+
title: 'Test Activity',
27+
description: 'Description of task',
28+
},
29+
});
30+
});
31+
});

0 commit comments

Comments
 (0)