File tree Expand file tree Collapse file tree 3 files changed +49
-6
lines changed Expand file tree Collapse file tree 3 files changed +49
-6
lines changed Original file line number Diff line number Diff line change @@ -562,7 +562,7 @@ class QueryBuilder extends QueryBuilderBase {
562
562
}
563
563
564
564
clone ( ) {
565
- const builder = new this . constructor ( this . modelClass ( ) ) ;
565
+ const builder = this . emptyInstance ( ) ;
566
566
567
567
// Call the super class's clone implementation.
568
568
this . baseCloneInto ( builder ) ;
@@ -577,13 +577,20 @@ class QueryBuilder extends QueryBuilderBase {
577
577
builder . _findOperationOptions = this . _findOperationOptions ;
578
578
builder . _relatedQueryFor = this . _relatedQueryFor ;
579
579
580
+ return builder ;
581
+ }
582
+
583
+ emptyInstance ( ) {
584
+ const builder = new this . constructor ( this . modelClass ( ) ) ;
585
+
580
586
builder . _findOperationFactory = this . _findOperationFactory ;
581
587
builder . _insertOperationFactory = this . _insertOperationFactory ;
582
588
builder . _updateOperationFactory = this . _updateOperationFactory ;
583
589
builder . _patchOperationFactory = this . _patchOperationFactory ;
584
590
builder . _relateOperationFactory = this . _relateOperationFactory ;
585
591
builder . _unrelateOperationFactory = this . _unrelateOperationFactory ;
586
592
builder . _deleteOperationFactory = this . _deleteOperationFactory ;
593
+ builder . _relatedQueryFor = this . _relatedQueryFor ;
587
594
588
595
return builder ;
589
596
}
Original file line number Diff line number Diff line change @@ -25,11 +25,11 @@ class UpdateAndFetchOperation extends DelegateOperation {
25
25
}
26
26
27
27
onBuild ( builder ) {
28
- super . onBuild ( builder ) ;
29
-
30
28
if ( ! this . skipIdWhere ) {
31
29
builder . findById ( this . id ) ;
32
30
}
31
+
32
+ super . onBuild ( builder ) ;
33
33
}
34
34
35
35
async onAfter2 ( builder , numUpdated ) {
@@ -40,10 +40,13 @@ class UpdateAndFetchOperation extends DelegateOperation {
40
40
}
41
41
42
42
const fetched = await builder
43
- . modelClass ( )
44
- . query ( )
43
+ . emptyInstance ( )
45
44
. childQueryOf ( builder )
46
- . findById ( this . id )
45
+ . modify ( builder => {
46
+ if ( ! this . skipIdWhere ) {
47
+ builder . findById ( this . id ) ;
48
+ }
49
+ } )
47
50
. castTo ( builder . resultModelClass ( ) ) ;
48
51
49
52
if ( fetched ) {
Original file line number Diff line number Diff line change @@ -1123,6 +1123,39 @@ module.exports = session => {
1123
1123
} ) ;
1124
1124
} ) ;
1125
1125
1126
+ it ( 'should patch a related object with extras using patchAndFetchById' , ( ) => {
1127
+ return Model1 . query ( )
1128
+ . findById ( 1 )
1129
+ . then ( parent => {
1130
+ return parent
1131
+ . $relatedQuery ( 'model1Relation3' )
1132
+ . debug ( )
1133
+ . patchAndFetchById ( 4 , {
1134
+ model2Prop1 : 'iam updated' ,
1135
+ extra1 : 'updated extra 1' ,
1136
+ // Test query properties. sqlite doesn't have `concat` function. Use a literal for it.
1137
+ extra2 : isSqlite ( session . knex )
1138
+ ? 'updated extra 2'
1139
+ : raw ( `CONCAT('updated extra ', '2')` )
1140
+ } ) ;
1141
+ } )
1142
+ . then ( result => {
1143
+ chai . expect ( result ) . to . containSubset ( {
1144
+ model2Prop1 : 'iam updated' ,
1145
+ extra1 : 'updated extra 1' ,
1146
+ extra2 : 'updated extra 2' ,
1147
+ idCol : 4
1148
+ } ) ;
1149
+
1150
+ return Model1 . query ( )
1151
+ . findById ( 1 )
1152
+ . eager ( 'model1Relation3' ) ;
1153
+ } )
1154
+ . then ( model1 => {
1155
+ console . dir ( model1 , { depth : null } ) ;
1156
+ } ) ;
1157
+ } ) ;
1158
+
1126
1159
it ( 'should patch a related object with extras' , ( ) => {
1127
1160
return Model1 . query ( )
1128
1161
. findById ( 1 )
You can’t perform that action at this time.
0 commit comments