@@ -284,9 +284,16 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
284284 if ( context . field ?. backLink ) {
285285 const backLinkField = resolveField ( this . modelMeta , model , context . field . backLink ) ;
286286 if ( backLinkField ?. isRelationOwner ) {
287- // the target side of relation owns the relation,
288- // check if it's updatable
289- await this . policyUtils . checkPolicyForUnique ( model , args . where , 'update' , db , args ) ;
287+ // "connect" is actually "update" to foreign keys, so we need to map the "connect" payload
288+ // to "update" payload by translating pk to fks, and use that to check update policies
289+ const fieldsToUpdate = Object . values ( backLinkField . foreignKeyMapping ?? { } ) ;
290+ await this . policyUtils . checkPolicyForUnique (
291+ model ,
292+ args . where ,
293+ 'update' ,
294+ db ,
295+ fieldsToUpdate
296+ ) ;
290297 }
291298 }
292299
@@ -319,9 +326,12 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
319326 // check existence
320327 await this . policyUtils . checkExistence ( db , model , args , true ) ;
321328
322- // the target side of relation owns the relation,
323- // check if it's updatable
324- await this . policyUtils . checkPolicyForUnique ( model , args , 'update' , db , args ) ;
329+ // the target side of relation owns the relation, check if it's updatable
330+
331+ // "connect" is actually "update" to foreign keys, so we need to map the "connect" payload
332+ // to "update" payload by translating pk to fks, and use that to check update policies
333+ const fieldsToUpdate = Object . values ( backLinkField . foreignKeyMapping ?? { } ) ;
334+ await this . policyUtils . checkPolicyForUnique ( model , args , 'update' , db , fieldsToUpdate ) ;
325335 }
326336 }
327337 } ,
@@ -909,21 +919,11 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
909919 }
910920
911921 // update happens on the related model, require updatable,
912- // translate args to foreign keys so field-level policies can be checked
913- const checkArgs : any = { } ;
914- if ( args && typeof args === 'object' && backLinkField . foreignKeyMapping ) {
915- for ( const key of Object . keys ( args ) ) {
916- const fk = backLinkField . foreignKeyMapping [ key ] ;
917- if ( fk ) {
918- checkArgs [ fk ] = args [ key ] ;
919- }
920- }
921- }
922-
923922 // `uniqueFilter` can be undefined if the entity to be disconnected doesn't exist
924923 if ( uniqueFilter ) {
925- // check for update
926- await this . policyUtils . checkPolicyForUnique ( model , uniqueFilter , 'update' , db , checkArgs ) ;
924+ // check for update, "connect" and "disconnect" are actually "update" to foreign keys
925+ const fieldsToUpdate = Object . values ( backLinkField . foreignKeyMapping ?? { } ) ;
926+ await this . policyUtils . checkPolicyForUnique ( model , uniqueFilter , 'update' , db , fieldsToUpdate ) ;
927927
928928 // register post-update check
929929 await _registerPostUpdateCheck ( model , uniqueFilter , uniqueFilter ) ;
@@ -971,12 +971,18 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
971971 this . policyUtils . tryReject ( db , this . model , 'update' ) ;
972972
973973 // check pre-update guard
974- await this . policyUtils . checkPolicyForUnique ( model , uniqueFilter , 'update' , db , args ) ;
974+ await this . policyUtils . checkPolicyForUnique (
975+ model ,
976+ uniqueFilter ,
977+ 'update' ,
978+ db ,
979+ this . queryUtils . getFieldsWithDefinedValues ( updatePayload )
980+ ) ;
975981
976982 // handle the case where id fields are updated
977983 const _args : any = args ;
978- const updatePayload = _args . data && typeof _args . data === 'object' ? _args . data : _args ;
979- const postUpdateIds = this . calculatePostUpdateIds ( model , existing , updatePayload ) ;
984+ const checkPayload = _args . data && typeof _args . data === 'object' ? _args . data : _args ;
985+ const postUpdateIds = this . calculatePostUpdateIds ( model , existing , checkPayload ) ;
980986
981987 // register post-update check
982988 await _registerPostUpdateCheck ( model , existing , postUpdateIds ) ;
@@ -1068,7 +1074,13 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
10681074 // update case
10691075
10701076 // check pre-update guard
1071- await this . policyUtils . checkPolicyForUnique ( model , existing , 'update' , db , args ) ;
1077+ await this . policyUtils . checkPolicyForUnique (
1078+ model ,
1079+ existing ,
1080+ 'update' ,
1081+ db ,
1082+ this . queryUtils . getFieldsWithDefinedValues ( args . update )
1083+ ) ;
10721084
10731085 // handle the case where id fields are updated
10741086 const postUpdateIds = this . calculatePostUpdateIds ( model , existing , args . update ) ;
@@ -1156,7 +1168,7 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
11561168 await this . policyUtils . checkExistence ( db , model , uniqueFilter , true ) ;
11571169
11581170 // check delete guard
1159- await this . policyUtils . checkPolicyForUnique ( model , uniqueFilter , 'delete' , db , args ) ;
1171+ await this . policyUtils . checkPolicyForUnique ( model , uniqueFilter , 'delete' , db , [ ] ) ;
11601172 } ,
11611173
11621174 deleteMany : async ( model , args , context ) => {
@@ -1526,7 +1538,7 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
15261538 await this . policyUtils . checkExistence ( tx , this . model , args . where , true ) ;
15271539
15281540 // inject delete guard
1529- await this . policyUtils . checkPolicyForUnique ( this . model , args . where , 'delete' , tx , args ) ;
1541+ await this . policyUtils . checkPolicyForUnique ( this . model , args . where , 'delete' , tx , [ ] ) ;
15301542
15311543 // proceed with the deletion
15321544 if ( this . shouldLogQuery ) {
@@ -1773,7 +1785,7 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
17731785 private async runPostWriteChecks ( postWriteChecks : PostWriteCheckRecord [ ] , db : CrudContract ) {
17741786 await Promise . all (
17751787 postWriteChecks . map ( async ( { model, operation, uniqueFilter, preValue } ) =>
1776- this . policyUtils . checkPolicyForUnique ( model , uniqueFilter , operation , db , undefined , preValue )
1788+ this . policyUtils . checkPolicyForUnique ( model , uniqueFilter , operation , db , [ ] , preValue )
17771789 )
17781790 ) ;
17791791 }
0 commit comments