@@ -34,7 +34,7 @@ import { getPrismaVersion } from '@zenstackhq/sdk/prisma';
3434import { match , P } from 'ts-pattern' ;
3535import { getIdFields } from '../../utils/ast-utils' ;
3636
37- import { DELEGATE_AUX_RELATION_PREFIX , PRISMA_MINIMUM_VERSION } from '@zenstackhq/runtime' ;
37+ import { DELEGATE_AUX_RELATION_PREFIX , PRISMA_MINIMUM_VERSION , truncate } from '@zenstackhq/runtime' ;
3838import {
3939 getAttribute ,
4040 getAttributeArg ,
@@ -82,10 +82,6 @@ const MODEL_PASSTHROUGH_ATTR = '@@prisma.passthrough';
8282const FIELD_PASSTHROUGH_ATTR = '@prisma.passthrough' ;
8383const PROVIDERS_SUPPORTING_NAMED_CONSTRAINTS = [ 'postgresql' , 'mysql' , 'cockroachdb' ] ;
8484
85- // Some database providers like postgres and mysql have default limit to the length of identifiers
86- // Here we use a conservative value that should work for most cases, and truncate names if needed
87- const IDENTIFIER_NAME_MAX_LENGTH = 50 - DELEGATE_AUX_RELATION_PREFIX . length ;
88-
8985/**
9086 * Generates Prisma schema file
9187 */
@@ -318,7 +314,7 @@ export class PrismaSchemaGenerator {
318314
319315 // generate an optional relation field in delegate base model to each concrete model
320316 concreteModels . forEach ( ( concrete ) => {
321- const auxName = `${ DELEGATE_AUX_RELATION_PREFIX } _${ this . truncate ( lowerCaseFirst ( concrete . name ) ) } ` ;
317+ const auxName = `${ DELEGATE_AUX_RELATION_PREFIX } _${ truncate ( lowerCaseFirst ( concrete . name ) ) } ` ;
322318 model . addField ( auxName , new ModelFieldType ( concrete . name , false , true ) ) ;
323319 } ) ;
324320 }
@@ -339,7 +335,7 @@ export class PrismaSchemaGenerator {
339335 const idFields = getIdFields ( base ) ;
340336
341337 // add relation fields
342- const relationField = `${ DELEGATE_AUX_RELATION_PREFIX } _${ this . truncate ( lowerCaseFirst ( base . name ) ) } ` ;
338+ const relationField = `${ DELEGATE_AUX_RELATION_PREFIX } _${ truncate ( lowerCaseFirst ( base . name ) ) } ` ;
343339 model . addField ( relationField , base . name , [
344340 new PrismaFieldAttribute ( '@relation' , [
345341 new PrismaAttributeArg (
@@ -405,7 +401,7 @@ export class PrismaSchemaGenerator {
405401 // e.g., delegate_aux_User_myAsset_Video
406402 const auxRelationName = `${ dataModel . name } _${ field . name } _${ concrete . name } ` ;
407403 const auxRelationField = model . addField (
408- `${ DELEGATE_AUX_RELATION_PREFIX } _${ this . truncate ( auxRelationName ) } ` ,
404+ `${ DELEGATE_AUX_RELATION_PREFIX } _${ truncate ( auxRelationName ) } ` ,
409405 new ModelFieldType ( concrete . name , field . type . array , field . type . optional )
410406 ) ;
411407
@@ -487,7 +483,7 @@ export class PrismaSchemaGenerator {
487483
488484 // fix its name
489485 const addedFkFieldName = `${ dataModel . name } _${ origForeignKey . name } _${ concreteModel . name } ` ;
490- addedFkField . name = `${ DELEGATE_AUX_RELATION_PREFIX } _${ this . truncate ( addedFkFieldName ) } ` ;
486+ addedFkField . name = `${ DELEGATE_AUX_RELATION_PREFIX } _${ truncate ( addedFkFieldName ) } ` ;
491487
492488 // we also need to make sure `@unique` constraint's `map` parameter is fixed to avoid conflict
493489 const uniqueAttr = addedFkField . attributes . find (
@@ -552,28 +548,6 @@ export class PrismaSchemaGenerator {
552548 }
553549 }
554550
555- private truncate ( name : string ) {
556- if ( name . length <= IDENTIFIER_NAME_MAX_LENGTH ) {
557- return name ;
558- }
559-
560- const shortName = name . slice ( 0 , IDENTIFIER_NAME_MAX_LENGTH ) ;
561- const entry = this . shortNameMap . get ( shortName ) ;
562- if ( ! entry ) {
563- this . shortNameMap . set ( shortName , [ name ] ) ;
564- return `${ shortName } _0` ;
565- } else {
566- const index = entry . findIndex ( ( n ) => n === name ) ;
567- if ( index >= 0 ) {
568- return `${ shortName } _${ index } ` ;
569- } else {
570- const newIndex = entry . length ;
571- entry . push ( name ) ;
572- return `${ shortName } _${ newIndex } ` ;
573- }
574- }
575- }
576-
577551 private nameRelationsInheritedFromDelegate ( model : PrismaDataModel , decl : DataModel ) {
578552 if ( this . mode !== 'logical' ) {
579553 return ;
@@ -620,7 +594,7 @@ export class PrismaSchemaGenerator {
620594 // relation name format: delegate_aux_[relationType]_[oppositeRelationField]_[concrete]
621595 const relAttr = getAttribute ( f , '@relation' ) ;
622596 const name = `${ fieldType . name } _${ oppositeRelationField . name } _${ decl . name } ` ;
623- const relName = `${ DELEGATE_AUX_RELATION_PREFIX } _${ this . truncate ( name ) } ` ;
597+ const relName = `${ DELEGATE_AUX_RELATION_PREFIX } _${ truncate ( name ) } ` ;
624598
625599 if ( relAttr ) {
626600 const nameArg = getAttributeArg ( relAttr , 'name' ) ;
0 commit comments