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

Skip to content

Commit a376005

Browse files
committed
fixes Vincit#1227
1 parent dcaf042 commit a376005

File tree

6 files changed

+144
-2
lines changed

6 files changed

+144
-2
lines changed

doc/includes/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.6.3
4+
5+
* Fixes: [#1227](https://github.com/Vincit/objection.js/issues/1227)
6+
37
## 1.6.2
48

59
* Add `as` method for `raw` making it possible to use `raw` expressions in `joinEager` modifiers (as long as you give names to your raw expressions using `as`).

lib/model/graph/ModelGraphNode.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ModelGraphNode {
1010
this.obj = obj;
1111
this.edges = [];
1212
this.userData = {};
13+
this.hadIdOriginally = obj.$hasId();
1314

1415
// These are also included in `edges`. These are simply
1516
// shortcuts for commonly used edges.

lib/queryBuilder/graph/GraphOptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class GraphOptions {
5050
return (
5151
!getCurrentNode(node, currentGraph) &&
5252
!this.shouldRelateIgnoreDisable(node, currentGraph) &&
53-
(!node.obj.$hasId() || this._hasOption(node, INSERT_MISSING))
53+
(!node.hadIdOriginally || this._hasOption(node, INSERT_MISSING))
5454
);
5555
}
5656

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "objection",
3-
"version": "1.6.2",
3+
"version": "1.6.3",
44
"description": "An SQL-friendly ORM for Node.js",
55
"main": "lib/objection.js",
66
"license": "MIT",
@@ -14,6 +14,7 @@
1414
"test-typings": "tsc",
1515
"coveralls": "cat ./testCoverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
1616
"perf": "mocha --slow 60000 --timeout 60000 --reporter spec --recursive perf",
17+
"perf-debug": "mocha --slow 60000 --timeout 60000 --reporter spec --inspect-brk perf",
1718
"perf-prof": "mocha --slow 60000 --timeout 60000 --reporter spec --recursive perf --prof",
1819
"perf-opt": "mocha --slow 60000 --timeout 60000 --reporter spec --recursive perf --trace_opt --trace_deopt",
1920
"prettier": "prettier --write \"{examples,lib,tests,typings}/**/*.{js,ts}\"",

tests/integration/misc/#1227.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const { Model } = require('../../../');
2+
const { expect } = require('chai');
3+
4+
module.exports = session => {
5+
describe(`primary key to primary key relations with upsertGraph #1227`, () => {
6+
let knex = session.knex;
7+
let Person, Animal;
8+
9+
before(() => {
10+
return knex.schema
11+
.dropTableIfExists('cousins')
12+
.dropTableIfExists('persons')
13+
.createTable('persons', table => {
14+
table.increments('id').primary();
15+
table.string('name');
16+
})
17+
.createTable('pets', table => {
18+
table
19+
.integer('id')
20+
.unsigned()
21+
.primary()
22+
.references('persons.id');
23+
table.string('name');
24+
});
25+
});
26+
27+
after(() => {
28+
return knex.schema.dropTableIfExists('pets').dropTableIfExists('persons');
29+
});
30+
31+
beforeEach(() => {
32+
Animal = class Animal extends Model {
33+
static get tableName() {
34+
return 'pets';
35+
}
36+
};
37+
38+
Animal.knex(knex);
39+
});
40+
41+
beforeEach(() => {
42+
Person = class Person extends Model {
43+
static get tableName() {
44+
return 'persons';
45+
}
46+
47+
static get relationMappings() {
48+
return {
49+
pet: {
50+
modelClass: Animal,
51+
relation: Model.HasOneRelation,
52+
join: {
53+
from: 'persons.id',
54+
to: 'pets.id'
55+
}
56+
}
57+
};
58+
}
59+
};
60+
61+
Person.knex(knex);
62+
});
63+
64+
beforeEach(() => Animal.query().delete());
65+
beforeEach(() => Person.query().delete());
66+
67+
it('should be able to insert a primary key to primary key hasOne relation', () => {
68+
return Person.query()
69+
.upsertGraph({
70+
name: 'person',
71+
pet: {
72+
name: 'pet'
73+
}
74+
})
75+
.then(person => {
76+
return Person.query()
77+
.findById(person.id)
78+
.eager('pet');
79+
})
80+
.then(person => {
81+
expect(person).to.containSubset({
82+
name: 'person',
83+
pet: {
84+
name: 'pet'
85+
}
86+
});
87+
});
88+
});
89+
});
90+
};

tests/integration/upsertGraph.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,52 @@ module.exports = session => {
446446
});
447447
});
448448

449+
it('should work with an empty object in belongsToOne relation', () => {
450+
const upsert = {
451+
model1Relation1: {}
452+
};
453+
454+
return transaction(session.knex, trx => Model1.query(trx).upsertGraph(upsert))
455+
.then(inserted =>
456+
Model1.query(session.knex)
457+
.findById(inserted.id)
458+
.eager('model1Relation1')
459+
)
460+
.then(model => {
461+
chai.expect(model).to.containSubset({
462+
model1Prop1: null,
463+
model1Prop2: null,
464+
model1Relation1: {
465+
model1Prop1: null,
466+
model1Prop2: null
467+
}
468+
});
469+
});
470+
});
471+
472+
it('should work with an empty object in hasOne relation', () => {
473+
const upsert = {
474+
model1Relation1Inverse: {}
475+
};
476+
477+
return transaction(session.knex, trx => Model1.query(trx).upsertGraph(upsert))
478+
.then(inserted =>
479+
Model1.query(session.knex)
480+
.findById(inserted.id)
481+
.eager('model1Relation1Inverse')
482+
)
483+
.then(model => {
484+
chai.expect(model).to.containSubset({
485+
model1Prop1: null,
486+
model1Prop2: null,
487+
model1Relation1Inverse: {
488+
model1Prop1: null,
489+
model1Prop2: null
490+
}
491+
});
492+
});
493+
});
494+
449495
it('should update model if the model changes and a belongsToOne relation changes', () => {
450496
const upsert = {
451497
id: 1,

0 commit comments

Comments
 (0)