@@ -5,10 +5,12 @@ import { ACTIONS_WITH_WRITE_PAYLOAD } from '../../constants';
5
5
import {
6
6
FieldInfo ,
7
7
NestedWriteVisitor ,
8
+ NestedWriteVisitorContext ,
8
9
PrismaWriteActionType ,
9
10
clone ,
10
11
enumerate ,
11
12
getFields ,
13
+ getModelInfo ,
12
14
getTypeDefInfo ,
13
15
requireField ,
14
16
} from '../../cross' ;
@@ -61,7 +63,7 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
61
63
private async preprocessWritePayload ( model : string , action : PrismaWriteActionType , args : any ) {
62
64
const newArgs = clone ( args ) ;
63
65
64
- const processCreatePayload = ( model : string , data : any ) => {
66
+ const processCreatePayload = ( model : string , data : any , context : NestedWriteVisitorContext ) => {
65
67
const fields = getFields ( this . options . modelMeta , model ) ;
66
68
for ( const fieldInfo of Object . values ( fields ) ) {
67
69
if ( fieldInfo . isTypeDef ) {
@@ -82,24 +84,24 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
82
84
const defaultValue = this . getDefaultValue ( fieldInfo ) ;
83
85
if ( defaultValue !== undefined ) {
84
86
// set field value extracted from `auth()`
85
- this . setDefaultValueForModelData ( fieldInfo , model , data , defaultValue ) ;
87
+ this . setDefaultValueForModelData ( fieldInfo , model , data , defaultValue , context ) ;
86
88
}
87
89
}
88
90
} ;
89
91
90
92
// visit create payload and set default value to fields using `auth()` in `@default()`
91
93
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 ) ;
94
96
} ,
95
97
96
- upsert : ( model , data ) => {
97
- processCreatePayload ( model , data . create ) ;
98
+ upsert : ( model , data , context ) => {
99
+ processCreatePayload ( model , data . create , context ) ;
98
100
} ,
99
101
100
- createMany : ( model , args ) => {
102
+ createMany : ( model , args , context ) => {
101
103
for ( const item of enumerate ( args . data ) ) {
102
- processCreatePayload ( model , item ) ;
104
+ processCreatePayload ( model , item , context ) ;
103
105
}
104
106
} ,
105
107
} ) ;
@@ -108,12 +110,32 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
108
110
return newArgs ;
109
111
}
110
112
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
+ ) {
112
120
if ( fieldInfo . isForeignKey && fieldInfo . relationField && fieldInfo . relationField in data ) {
113
121
// if the field is a fk, and the relation field is already set, we should not override it
114
122
return ;
115
123
}
116
124
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
+
117
139
if ( fieldInfo . isForeignKey && ! isUnsafeMutate ( model , data , this . options . modelMeta ) ) {
118
140
// if the field is a fk, and the create payload is not unsafe, we need to translate
119
141
// the fk field setting to a `connect` of the corresponding relation field
0 commit comments