@@ -34,6 +34,7 @@ import { getIdFields } from '../../utils/ast-utils';
3434import { DELEGATE_AUX_RELATION_PREFIX , PRISMA_MINIMUM_VERSION } from '@zenstackhq/runtime' ;
3535import {
3636 getAttribute ,
37+ getAttributeArg ,
3738 getForeignKeyFields ,
3839 getLiteral ,
3940 getPrismaVersion ,
@@ -299,6 +300,9 @@ export class PrismaSchemaGenerator {
299300
300301 // expand relations on other models that reference delegated models to concrete models
301302 this . expandPolymorphicRelations ( model , decl ) ;
303+
304+ // name relations inherited from delegate base models for disambiguation
305+ this . nameRelationsInheritedFromDelegate ( model , decl ) ;
302306 }
303307
304308 private generateDelegateRelationForBase ( model : PrismaDataModel , decl : DataModel ) {
@@ -422,6 +426,8 @@ export class PrismaSchemaGenerator {
422426 ) ;
423427
424428 const addedRel = new PrismaFieldAttribute ( '@relation' , [
429+ // use field name as relation name for disambiguation
430+ new PrismaAttributeArg ( undefined , new AttributeArgValue ( 'String' , relationField . name ) ) ,
425431 new PrismaAttributeArg ( 'fields' , args ) ,
426432 new PrismaAttributeArg ( 'references' , args ) ,
427433 ] ) ;
@@ -440,11 +446,60 @@ export class PrismaSchemaGenerator {
440446 } else {
441447 relationField . attributes . push ( this . makeFieldAttribute ( relAttr as DataModelFieldAttribute ) ) ;
442448 }
449+ } else {
450+ relationField . attributes . push (
451+ new PrismaFieldAttribute ( '@relation' , [
452+ // use field name as relation name for disambiguation
453+ new PrismaAttributeArg ( undefined , new AttributeArgValue ( 'String' , relationField . name ) ) ,
454+ ] )
455+ ) ;
443456 }
444457 } ) ;
445458 } ) ;
446459 }
447460
461+ private nameRelationsInheritedFromDelegate ( model : PrismaDataModel , decl : DataModel ) {
462+ if ( this . mode !== 'logical' ) {
463+ return ;
464+ }
465+
466+ // the logical schema needs to name relations inherited from delegate base models for disambiguation
467+
468+ decl . fields . forEach ( ( f ) => {
469+ if ( ! f . $inheritedFrom || ! isDelegateModel ( f . $inheritedFrom ) || ! isDataModel ( f . type . reference ?. ref ) ) {
470+ return ;
471+ }
472+
473+ const prismaField = model . fields . find ( ( field ) => field . name === f . name ) ;
474+ if ( ! prismaField ) {
475+ return ;
476+ }
477+
478+ const relAttr = getAttribute ( f , '@relation' ) ;
479+ const relName = `${ DELEGATE_AUX_RELATION_PREFIX } _${ lowerCaseFirst ( decl . name ) } ` ;
480+
481+ if ( relAttr ) {
482+ const nameArg = getAttributeArg ( relAttr , 'name' ) ;
483+ if ( ! nameArg ) {
484+ const prismaRelAttr = prismaField . attributes . find (
485+ ( attr ) => ( attr as PrismaFieldAttribute ) . name === '@relation'
486+ ) as PrismaFieldAttribute ;
487+ if ( prismaRelAttr ) {
488+ prismaRelAttr . args . unshift (
489+ new PrismaAttributeArg ( undefined , new AttributeArgValue ( 'String' , relName ) )
490+ ) ;
491+ }
492+ }
493+ } else {
494+ prismaField . attributes . push (
495+ new PrismaFieldAttribute ( '@relation' , [
496+ new PrismaAttributeArg ( undefined , new AttributeArgValue ( 'String' , relName ) ) ,
497+ ] )
498+ ) ;
499+ }
500+ } ) ;
501+ }
502+
448503 private get supportNamedConstraints ( ) {
449504 const ds = this . zmodel . declarations . find ( isDataSource ) ;
450505 if ( ! ds ) {
0 commit comments