diff --git a/packages/runtime/src/enhancements/delegate.ts b/packages/runtime/src/enhancements/delegate.ts index edbaed78d..ea31fe02d 100644 --- a/packages/runtime/src/enhancements/delegate.ts +++ b/packages/runtime/src/enhancements/delegate.ts @@ -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); @@ -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); diff --git a/tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts b/tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts index 8acc832c6..ea9b8efca 100644 --- a/tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts +++ b/tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts @@ -158,6 +158,22 @@ describe('Polymorphism Test', () => { ).resolves.toMatchObject({ count: 2 }); }); + it('create concrete with explicit id', async () => { + const { enhance } = await loadSchema(schema, { enhancements: ['delegate'] }); + const db = enhance(); + + await expect( + db.ratedVideo.create({ data: { id: 1, duration: 100, url: 'xyz', rating: 5 } }) + ).resolves.toMatchObject({ + id: 1, + duration: 100, + url: 'xyz', + rating: 5, + assetType: 'Video', + videoType: 'RatedVideo', + }); + }); + it('read with concrete', async () => { const { db, user, video } = await setup(); diff --git a/tests/regression/tests/issue-1518.test.ts b/tests/regression/tests/issue-1518.test.ts new file mode 100644 index 000000000..83517a5ca --- /dev/null +++ b/tests/regression/tests/issue-1518.test.ts @@ -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', + }, + }); + }); +});