File tree Expand file tree Collapse file tree 4 files changed +65
-6
lines changed
schema/src/plugins/prisma Expand file tree Collapse file tree 4 files changed +65
-6
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ import {
39
39
getAttribute ,
40
40
getAttributeArg ,
41
41
getAttributeArgLiteral ,
42
+ getInheritedFromDelegate ,
42
43
getLiteral ,
43
44
getRelationKeyPairs ,
44
45
isDelegateModel ,
@@ -261,9 +262,10 @@ export class PrismaSchemaGenerator {
261
262
const model = decl . isView ? prisma . addView ( decl . name ) : prisma . addModel ( decl . name ) ;
262
263
for ( const field of decl . fields ) {
263
264
if ( field . $inheritedFrom ) {
265
+ const inheritedFromDelegate = getInheritedFromDelegate ( field ) ;
264
266
if (
265
- // abstract inheritance is always kept
266
- field . $inheritedFrom . isAbstract ||
267
+ // fields inherited from delegate are excluded from physical schema
268
+ ! inheritedFromDelegate ||
267
269
// logical schema keeps all inherited fields
268
270
this . mode === 'logical' ||
269
271
// id fields are always kept
Original file line number Diff line number Diff line change @@ -24,16 +24,16 @@ import {
24
24
getAttributeArgs ,
25
25
getAuthModel ,
26
26
getDataModels ,
27
+ getInheritedFromDelegate ,
27
28
getLiteral ,
29
+ getRelationField ,
28
30
hasAttribute ,
29
- isDelegateModel ,
30
31
isAuthInvocation ,
31
32
isEnumFieldReference ,
32
33
isForeignKeyField ,
33
34
isIdField ,
34
35
resolved ,
35
36
TypeScriptExpressionTransformer ,
36
- getRelationField ,
37
37
} from '.' ;
38
38
39
39
/**
@@ -267,9 +267,10 @@ function writeFields(
267
267
defaultValueProvider: ${ defaultValueProvider } ,` ) ;
268
268
}
269
269
270
- if ( f . $inheritedFrom && isDelegateModel ( f . $inheritedFrom ) && ! isIdField ( f ) ) {
270
+ const inheritedFromDelegate = getInheritedFromDelegate ( f ) ;
271
+ if ( inheritedFromDelegate && ! isIdField ( f ) ) {
271
272
writer . write ( `
272
- inheritedFrom: ${ JSON . stringify ( f . $inheritedFrom . name ) } ,` ) ;
273
+ inheritedFrom: ${ JSON . stringify ( inheritedFromDelegate . name ) } ,` ) ;
273
274
}
274
275
275
276
if ( isAutoIncrement ( f ) ) {
Original file line number Diff line number Diff line change @@ -554,3 +554,18 @@ export function getDataSourceProvider(model: Model) {
554
554
}
555
555
return getLiteral < string > ( provider . value ) ;
556
556
}
557
+
558
+ /**
559
+ * Finds the original delegate base model that defines the given field.
560
+ */
561
+ export function getInheritedFromDelegate ( field : DataModelField ) {
562
+ if ( ! field . $inheritedFrom ) {
563
+ return undefined ;
564
+ }
565
+
566
+ // find the original base delegate model that defines this field,
567
+ // use `findLast` to start from the uppermost base
568
+ const bases = getRecursiveBases ( field . $container as DataModel , true ) ;
569
+ const foundBase = bases . findLast ( ( base ) => base . fields . some ( ( f ) => f . name === field . name ) && isDelegateModel ( base ) ) ;
570
+ return foundBase ;
571
+ }
Original file line number Diff line number Diff line change
1
+ import { loadSchema } from '@zenstackhq/testtools' ;
2
+ describe ( 'issue 1560' , ( ) => {
3
+ it ( 'regression' , async ( ) => {
4
+ const { enhance } = await loadSchema (
5
+ `
6
+ model User {
7
+ id String @id @default(cuid())
8
+ name String
9
+ ownedItems OwnedItem[]
10
+ }
11
+
12
+ abstract model Base {
13
+ id String @id @default(cuid())
14
+ ownerId String
15
+ owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
16
+ }
17
+
18
+ model OwnedItem extends Base {
19
+ ownedItemType String
20
+ @@delegate(ownedItemType)
21
+ }
22
+
23
+ model List extends OwnedItem {
24
+ title String
25
+ }
26
+ ` ,
27
+ { enhancements : [ 'delegate' ] }
28
+ ) ;
29
+
30
+ const db = enhance ( ) ;
31
+ await db . user . create ( { data : { id : '1' , name : 'user1' } } ) ;
32
+ await expect (
33
+ db . list . create ( { data : { id : '1' , title : 'list1' , owner : { connect : { id : '1' } } } } )
34
+ ) . resolves . toMatchObject ( {
35
+ id : '1' ,
36
+ title : 'list1' ,
37
+ ownerId : '1' ,
38
+ ownedItemType : 'List' ,
39
+ } ) ;
40
+ } ) ;
41
+ } ) ;
You can’t perform that action at this time.
0 commit comments