@@ -32,7 +32,7 @@ import {
32
32
} from '@zenstackhq/language/ast' ;
33
33
import { getIdFields } from '@zenstackhq/sdk' ;
34
34
import { getPrismaVersion } from '@zenstackhq/sdk/prisma' ;
35
- import { match } from 'ts-pattern' ;
35
+ import { match , P } from 'ts-pattern' ;
36
36
37
37
import { DELEGATE_AUX_RELATION_PREFIX , PRISMA_MINIMUM_VERSION } from '@zenstackhq/runtime' ;
38
38
import {
@@ -869,9 +869,34 @@ export class PrismaSchemaGenerator {
869
869
const docs = [ ...field . comments , ...this . getCustomAttributesAsComments ( field ) ] ;
870
870
const result = model . addField ( field . name , type , attributes , docs , addToFront ) ;
871
871
872
+ if ( this . mode === 'logical' ) {
873
+ if ( field . attributes . some ( ( attr ) => isDefaultWithAuth ( attr ) ) ) {
874
+ // field has `@default` with `auth()`, turn it into a dummy default value, and the
875
+ // real default value setting is handled outside Prisma
876
+ this . setDummyDefault ( result , field ) ;
877
+ }
878
+ }
879
+
872
880
return result ;
873
881
}
874
882
883
+ private setDummyDefault ( result : ModelField , field : DataModelField ) {
884
+ const dummyDefaultValue = match ( field . type . type )
885
+ . with ( 'String' , ( ) => new AttributeArgValue ( 'String' , '' ) )
886
+ . with ( P . union ( 'Int' , 'BigInt' , 'Float' , 'Decimal' ) , ( ) => new AttributeArgValue ( 'Number' , '0' ) )
887
+ . with ( 'Boolean' , ( ) => new AttributeArgValue ( 'Boolean' , 'false' ) )
888
+ . with ( 'DateTime' , ( ) => new AttributeArgValue ( 'FunctionCall' , new PrismaFunctionCall ( 'now' ) ) )
889
+ . with ( 'Json' , ( ) => new AttributeArgValue ( 'String' , '{}' ) )
890
+ . with ( 'Bytes' , ( ) => new AttributeArgValue ( 'String' , '' ) )
891
+ . otherwise ( ( ) => {
892
+ throw new PluginError ( name , `Unsupported field type with default value: ${ field . type . type } ` ) ;
893
+ } ) ;
894
+
895
+ result . attributes . push (
896
+ new PrismaFieldAttribute ( '@default' , [ new PrismaAttributeArg ( undefined , dummyDefaultValue ) ] )
897
+ ) ;
898
+ }
899
+
875
900
private ensureSupportingTypeDefFields ( zmodel : Model ) {
876
901
const dsProvider = getDataSourceProvider ( zmodel ) ;
877
902
if ( dsProvider && ! PROVIDERS_SUPPORTING_TYPEDEF_FIELDS . includes ( dsProvider ) ) {
0 commit comments