@@ -227,11 +227,19 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
227227    throw  new  Error ( 'Method not implemented.' ) ; 
228228  } 
229229
230-   async  checkUnique ( resource : AdminForthResource ,  column : AdminForthResourceColumn ,  value : any )  { 
230+   async  checkUnique ( resource : AdminForthResource ,  column : AdminForthResourceColumn ,  value : any ,   record ?:  any ) :  Promise < boolean >  { 
231231    process . env . HEAVY_DEBUG  &&  console . log ( '☝️🪲🪲🪲🪲 checkUnique|||' ,  column ,  value ) ; 
232+ 
233+     const  primaryKeyField  =  this . getPrimaryKey ( resource ) ; 
232234    const  existingRecord  =  await  this . getData ( { 
233235      resource, 
234-       filters : {  operator : AdminForthFilterOperators . AND ,  subFilters : [ {  field : column . name ,  operator : AdminForthFilterOperators . EQ ,  value } ] } , 
236+       filters : {  
237+         operator : AdminForthFilterOperators . AND ,  
238+         subFilters : [ 
239+           {  field : column . name ,  operator : AdminForthFilterOperators . EQ ,  value } , 
240+           ...( record  ? [ {  field : primaryKeyField ,  operator : AdminForthFilterOperators . NE  as  AdminForthFilterOperators . NE ,  value : record [ primaryKeyField ]  } ]  : [ ] ) 
241+         ] 
242+       } , 
235243      limit : 1 , 
236244      sort : [ ] , 
237245      offset : 0 , 
@@ -306,7 +314,7 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
306314  async  updateRecord ( {  resource,  recordId,  newValues } : {  resource : AdminForthResource ;  recordId : string ;  newValues : any ;  } ) : Promise < {  error ?: string ;  ok : boolean ;  } >  { 
307315    // transform value using setFieldValue and call updateRecordOriginalValues 
308316    const  recordWithOriginalValues  =  { ...newValues } ; 
309- 
317+      
310318    for  ( const  field  of  Object . keys ( newValues ) )  { 
311319      const  col  =  resource . dataSourceColumns . find ( ( col )  =>  col . name  ==  field ) ; 
312320      // todo instead of throwing error, we can just not use setFieldValue here, and pass original value to updateRecordOriginalValues 
@@ -319,12 +327,12 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
319327      } 
320328      recordWithOriginalValues [ col . name ]  =  this . setFieldValue ( col ,  newValues [ col . name ] ) ; 
321329    } 
322-     
330+     const   record   =   await   this . getRecordByPrimaryKey ( resource ,   recordId ) ; 
323331    let  error : string  |  null  =  null ; 
324332     await  Promise . all ( 
325333      resource . dataSourceColumns . map ( async  ( col )  =>  { 
326334        if  ( col . isUnique  &&  ! col . virtual  &&  ! error )  { 
327-           const  exists  =  await  this . checkUnique ( resource ,  col ,  recordWithOriginalValues [ col . name ] ) ; 
335+           const  exists  =  await  this . checkUnique ( resource ,  col ,  recordWithOriginalValues [ col . name ] ,   record ) ; 
328336          if  ( exists )  { 
329337            error  =  `Record with ${ col . name } ${ recordWithOriginalValues [ col . name ] }  ; 
330338          } 
0 commit comments