Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5007b74

Browse files
committed
fixes Vincit#1364
1 parent 636e15c commit 5007b74

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

lib/queryBuilder/QueryBuilder.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ class QueryBuilder extends QueryBuilderBase {
562562
}
563563

564564
clone() {
565-
const builder = new this.constructor(this.modelClass());
565+
const builder = this.emptyInstance();
566566

567567
// Call the super class's clone implementation.
568568
this.baseCloneInto(builder);
@@ -577,13 +577,20 @@ class QueryBuilder extends QueryBuilderBase {
577577
builder._findOperationOptions = this._findOperationOptions;
578578
builder._relatedQueryFor = this._relatedQueryFor;
579579

580+
return builder;
581+
}
582+
583+
emptyInstance() {
584+
const builder = new this.constructor(this.modelClass());
585+
580586
builder._findOperationFactory = this._findOperationFactory;
581587
builder._insertOperationFactory = this._insertOperationFactory;
582588
builder._updateOperationFactory = this._updateOperationFactory;
583589
builder._patchOperationFactory = this._patchOperationFactory;
584590
builder._relateOperationFactory = this._relateOperationFactory;
585591
builder._unrelateOperationFactory = this._unrelateOperationFactory;
586592
builder._deleteOperationFactory = this._deleteOperationFactory;
593+
builder._relatedQueryFor = this._relatedQueryFor;
587594

588595
return builder;
589596
}

lib/queryBuilder/operations/UpdateAndFetchOperation.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ class UpdateAndFetchOperation extends DelegateOperation {
2525
}
2626

2727
onBuild(builder) {
28-
super.onBuild(builder);
29-
3028
if (!this.skipIdWhere) {
3129
builder.findById(this.id);
3230
}
31+
32+
super.onBuild(builder);
3333
}
3434

3535
async onAfter2(builder, numUpdated) {
@@ -40,10 +40,13 @@ class UpdateAndFetchOperation extends DelegateOperation {
4040
}
4141

4242
const fetched = await builder
43-
.modelClass()
44-
.query()
43+
.emptyInstance()
4544
.childQueryOf(builder)
46-
.findById(this.id)
45+
.modify(builder => {
46+
if (!this.skipIdWhere) {
47+
builder.findById(this.id);
48+
}
49+
})
4750
.castTo(builder.resultModelClass());
4851

4952
if (fetched) {

tests/integration/patch.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,39 @@ module.exports = session => {
11231123
});
11241124
});
11251125

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+
11261159
it('should patch a related object with extras', () => {
11271160
return Model1.query()
11281161
.findById(1)

0 commit comments

Comments
 (0)