@@ -942,6 +942,18 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
942942 const modelDef = this . requireModel ( model ) ;
943943 let finalData = data ;
944944
945+ // fill in automatically updated fields
946+ const autoUpdatedFields : string [ ] = [ ] ;
947+ for ( const [ fieldName , fieldDef ] of Object . entries ( modelDef . fields ) ) {
948+ if ( fieldDef . updatedAt ) {
949+ if ( finalData === data ) {
950+ finalData = clone ( data ) ;
951+ }
952+ finalData [ fieldName ] = this . dialect . transformPrimitive ( new Date ( ) , 'DateTime' , false ) ;
953+ autoUpdatedFields . push ( fieldName ) ;
954+ }
955+ }
956+
945957 if ( Object . keys ( finalData ) . length === 0 ) {
946958 // nothing to update, return the original filter so that caller can identify the entity
947959 return combinedWhere ;
@@ -1018,20 +1030,13 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
10181030 }
10191031 }
10201032
1021- // fill in automatically updated fields
1022- const scalarFields = Object . values ( modelDef . fields )
1023- . filter ( ( f ) => ! f . relation )
1024- . map ( ( f ) => f . name ) ;
1025- if ( Object . keys ( updateFields ) . some ( ( f ) => scalarFields . includes ( f ) ) ) {
1026- // if any scalar fields are being updated, also update the `updatedAt` fields
1027- for ( const [ fieldName , fieldDef ] of Object . entries ( modelDef . fields ) ) {
1028- if ( fieldDef . updatedAt ) {
1029- updateFields [ fieldName ] = this . dialect . transformPrimitive ( new Date ( ) , 'DateTime' , false ) ;
1030- }
1031- }
1033+ let hasFieldUpdate = Object . keys ( updateFields ) . length > 0 ;
1034+ if ( hasFieldUpdate ) {
1035+ // check if only updating auto-updated fields, if so, we can skip the update
1036+ hasFieldUpdate = Object . keys ( updateFields ) . some ( ( f ) => ! autoUpdatedFields . includes ( f ) ) ;
10321037 }
10331038
1034- if ( Object . keys ( updateFields ) . length === 0 ) {
1039+ if ( ! hasFieldUpdate ) {
10351040 // nothing to update, return the filter so that the caller can identify the entity
10361041 return combinedWhere ;
10371042 } else {
0 commit comments