@@ -5,10 +5,12 @@ import { ACTIONS_WITH_WRITE_PAYLOAD } from '../../constants';
55import {
66 FieldInfo ,
77 NestedWriteVisitor ,
8+ NestedWriteVisitorContext ,
89 PrismaWriteActionType ,
910 clone ,
1011 enumerate ,
1112 getFields ,
13+ getModelInfo ,
1214 getTypeDefInfo ,
1315 requireField ,
1416} from '../../cross' ;
@@ -61,7 +63,7 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
6163 private async preprocessWritePayload ( model : string , action : PrismaWriteActionType , args : any ) {
6264 const newArgs = clone ( args ) ;
6365
64- const processCreatePayload = ( model : string , data : any ) => {
66+ const processCreatePayload = ( model : string , data : any , context : NestedWriteVisitorContext ) => {
6567 const fields = getFields ( this . options . modelMeta , model ) ;
6668 for ( const fieldInfo of Object . values ( fields ) ) {
6769 if ( fieldInfo . isTypeDef ) {
@@ -82,24 +84,24 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
8284 const defaultValue = this . getDefaultValue ( fieldInfo ) ;
8385 if ( defaultValue !== undefined ) {
8486 // set field value extracted from `auth()`
85- this . setDefaultValueForModelData ( fieldInfo , model , data , defaultValue ) ;
87+ this . setDefaultValueForModelData ( fieldInfo , model , data , defaultValue , context ) ;
8688 }
8789 }
8890 } ;
8991
9092 // visit create payload and set default value to fields using `auth()` in `@default()`
9193 const visitor = new NestedWriteVisitor ( this . options . modelMeta , {
92- create : ( model , data ) => {
93- processCreatePayload ( model , data ) ;
94+ create : ( model , data , context ) => {
95+ processCreatePayload ( model , data , context ) ;
9496 } ,
9597
96- upsert : ( model , data ) => {
97- processCreatePayload ( model , data . create ) ;
98+ upsert : ( model , data , context ) => {
99+ processCreatePayload ( model , data . create , context ) ;
98100 } ,
99101
100- createMany : ( model , args ) => {
102+ createMany : ( model , args , context ) => {
101103 for ( const item of enumerate ( args . data ) ) {
102- processCreatePayload ( model , item ) ;
104+ processCreatePayload ( model , item , context ) ;
103105 }
104106 } ,
105107 } ) ;
@@ -108,12 +110,32 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
108110 return newArgs ;
109111 }
110112
111- private setDefaultValueForModelData ( fieldInfo : FieldInfo , model : string , data : any , authDefaultValue : unknown ) {
113+ private setDefaultValueForModelData (
114+ fieldInfo : FieldInfo ,
115+ model : string ,
116+ data : any ,
117+ authDefaultValue : unknown ,
118+ context : NestedWriteVisitorContext
119+ ) {
112120 if ( fieldInfo . isForeignKey && fieldInfo . relationField && fieldInfo . relationField in data ) {
113121 // if the field is a fk, and the relation field is already set, we should not override it
114122 return ;
115123 }
116124
125+ if ( context . field ?. backLink ) {
126+ const modelInfo = getModelInfo ( this . options . modelMeta , model ) ;
127+ const parentModel = modelInfo ?. fields [ context . field . backLink ] ;
128+
129+ if (
130+ parentModel ?. isDataModel &&
131+ parentModel . foreignKeyMapping &&
132+ Object . keys ( parentModel . foreignKeyMapping ) . includes ( fieldInfo . name )
133+ ) {
134+ // if the field is part of a fk as part of a nested write, then prisma handles setting it
135+ return ;
136+ }
137+ }
138+
117139 if ( fieldInfo . isForeignKey && ! isUnsafeMutate ( model , data , this . options . modelMeta ) ) {
118140 // if the field is a fk, and the create payload is not unsafe, we need to translate
119141 // the fk field setting to a `connect` of the corresponding relation field
0 commit comments