@@ -101,8 +101,8 @@ export class PrismaSchemaGenerator {
101101
102102 private mode : 'logical' | 'physical' = 'physical' ;
103103
104- // a mapping from shortened names to their original full names
105- private shortNameMap = new Map < string , string [ ] > ( ) ;
104+ // a mapping from full names to shortened names
105+ private shortNameMap = new Map < string , string > ( ) ;
106106
107107 constructor ( private readonly zmodel : Model ) { }
108108
@@ -160,7 +160,7 @@ export class PrismaSchemaGenerator {
160160 }
161161 }
162162
163- return warnings ;
163+ return { warnings, shortNameMap : this . shortNameMap } ;
164164 }
165165
166166 private generateDataSource ( prisma : PrismaModel , dataSource : DataSource ) {
@@ -318,7 +318,7 @@ export class PrismaSchemaGenerator {
318318
319319 // generate an optional relation field in delegate base model to each concrete model
320320 concreteModels . forEach ( ( concrete ) => {
321- const auxName = `${ DELEGATE_AUX_RELATION_PREFIX } _${ this . truncate ( lowerCaseFirst ( concrete . name ) ) } ` ;
321+ const auxName = this . truncate ( `${ DELEGATE_AUX_RELATION_PREFIX } _${ lowerCaseFirst ( concrete . name ) } ` ) ;
322322 model . addField ( auxName , new ModelFieldType ( concrete . name , false , true ) ) ;
323323 } ) ;
324324 }
@@ -339,7 +339,7 @@ export class PrismaSchemaGenerator {
339339 const idFields = getIdFields ( base ) ;
340340
341341 // add relation fields
342- const relationField = `${ DELEGATE_AUX_RELATION_PREFIX } _${ this . truncate ( lowerCaseFirst ( base . name ) ) } ` ;
342+ const relationField = this . truncate ( `${ DELEGATE_AUX_RELATION_PREFIX } _${ lowerCaseFirst ( base . name ) } ` ) ;
343343 model . addField ( relationField , base . name , [
344344 new PrismaFieldAttribute ( '@relation' , [
345345 new PrismaAttributeArg (
@@ -403,9 +403,11 @@ export class PrismaSchemaGenerator {
403403 concreteModels . forEach ( ( concrete ) => {
404404 // aux relation name format: delegate_aux_[model]_[relationField]_[concrete]
405405 // e.g., delegate_aux_User_myAsset_Video
406- const auxRelationName = `${ dataModel . name } _${ field . name } _${ concrete . name } ` ;
406+ const auxRelationName = this . truncate (
407+ `${ DELEGATE_AUX_RELATION_PREFIX } _${ dataModel . name } _${ field . name } _${ concrete . name } `
408+ ) ;
407409 const auxRelationField = model . addField (
408- ` ${ DELEGATE_AUX_RELATION_PREFIX } _ ${ this . truncate ( auxRelationName ) } ` ,
410+ auxRelationName ,
409411 new ModelFieldType ( concrete . name , field . type . array , field . type . optional )
410412 ) ;
411413
@@ -493,15 +495,15 @@ export class PrismaSchemaGenerator {
493495
494496 // fix its name
495497 const addedFkFieldName = `${ dataModel . name } _${ origForeignKey . name } _${ concreteModel . name } ` ;
496- addedFkField . name = `${ DELEGATE_AUX_RELATION_PREFIX } _${ this . truncate ( addedFkFieldName ) } ` ;
498+ addedFkField . name = this . truncate ( `${ DELEGATE_AUX_RELATION_PREFIX } _${ addedFkFieldName } ` ) ;
497499
498500 // we also need to make sure `@unique` constraint's `map` parameter is fixed to avoid conflict
499501 const uniqueAttr = addedFkField . attributes . find (
500502 ( attr ) => ( attr as PrismaFieldAttribute ) . name === '@unique'
501503 ) as PrismaFieldAttribute ;
502504 if ( uniqueAttr ) {
503505 const mapArg = uniqueAttr . args . find ( ( arg ) => arg . name === 'map' ) ;
504- const constraintName = `${ addedFkField . name } _unique` ;
506+ const constraintName = this . truncate ( `${ addedFkField . name } _unique` ) ;
505507 if ( mapArg ) {
506508 mapArg . value = new AttributeArgValue ( 'String' , constraintName ) ;
507509 } else {
@@ -563,21 +565,29 @@ export class PrismaSchemaGenerator {
563565 return name ;
564566 }
565567
566- const shortName = name . slice ( 0 , IDENTIFIER_NAME_MAX_LENGTH ) ;
567- const entry = this . shortNameMap . get ( shortName ) ;
568- if ( ! entry ) {
569- this . shortNameMap . set ( shortName , [ name ] ) ;
570- return `${ shortName } _0` ;
571- } else {
572- const index = entry . findIndex ( ( n ) => n === name ) ;
573- if ( index >= 0 ) {
574- return `${ shortName } _${ index } ` ;
575- } else {
576- const newIndex = entry . length ;
577- entry . push ( name ) ;
578- return `${ shortName } _${ newIndex } ` ;
568+ const existing = this . shortNameMap . get ( name ) ;
569+ if ( existing ) {
570+ return existing ;
571+ }
572+
573+ const baseName = name . slice ( 0 , IDENTIFIER_NAME_MAX_LENGTH ) ;
574+ let index = 0 ;
575+ let shortName = `${ baseName } _${ index } ` ;
576+
577+ // eslint-disable-next-line no-constant-condition
578+ while ( true ) {
579+ const conflict = Array . from ( this . shortNameMap . values ( ) ) . find ( ( v ) => v === shortName ) ;
580+ if ( ! conflict ) {
581+ this . shortNameMap . set ( name , shortName ) ;
582+ break ;
579583 }
584+
585+ // try next index
586+ index ++ ;
587+ shortName = `${ baseName } _${ index } ` ;
580588 }
589+
590+ return shortName ;
581591 }
582592
583593 private nameRelationsInheritedFromDelegate ( model : PrismaDataModel , decl : DataModel ) {
@@ -626,7 +636,7 @@ export class PrismaSchemaGenerator {
626636 // relation name format: delegate_aux_[relationType]_[oppositeRelationField]_[concrete]
627637 const relAttr = getAttribute ( f , '@relation' ) ;
628638 const name = `${ fieldType . name } _${ oppositeRelationField . name } _${ decl . name } ` ;
629- const relName = `${ DELEGATE_AUX_RELATION_PREFIX } _${ this . truncate ( name ) } ` ;
639+ const relName = this . truncate ( `${ DELEGATE_AUX_RELATION_PREFIX } _${ name } ` ) ;
630640
631641 if ( relAttr ) {
632642 const nameArg = getAttributeArg ( relAttr , 'name' ) ;
0 commit comments