Skip to content

Commit 29e8f86

Browse files
committed
Fix shell model hasOne bug
1 parent 3e78e1e commit 29e8f86

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

lib/Associations/One.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
132132
cb(null);
133133
}
134134
} else {
135-
if (!Instance.hasOwnProperty(association.field)) {
136-
association.model.get(Instance[Model.id], function (err, instance) {
135+
if (Instance.isShell()) {
136+
Model.get(Instance[Model.id], function (err, instance) {
137137
if (err || !instance[association.field]) {
138138
return cb(null);
139139
}

lib/Instance.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,17 @@ function Instance(opts) {
478478
enumerable: false
479479
});
480480
Object.defineProperty(instance, "isPersisted", {
481-
value: function() {
481+
value: function () {
482482
return !opts.is_new;
483483
},
484484
enumerable: false
485485
});
486+
Object.defineProperty(instance, "isShell", {
487+
value: function () {
488+
return opts.isShell;
489+
},
490+
enumerable: false
491+
});
486492
Object.defineProperty(instance, "validate", {
487493
value: function (cb) {
488494
handleValidations(cb);

lib/Model.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function Model(opts) {
6666
id : opts.id,
6767
keys : opts.keys,
6868
is_new : inst_opts.is_new || false,
69+
isShell : inst_opts.isShell || false,
6970
data : data,
7071
autoSave : inst_opts.autoSave || false,
7172
extra : inst_opts.extra,
@@ -108,7 +109,7 @@ function Model(opts) {
108109
var data2 = {};
109110
data2[opts.id] = data;
110111

111-
return createInstance(data2);
112+
return createInstance(data2, { isShell: true });
112113
} else if (typeof data == "undefined") {
113114
data = {};
114115
}

test/integration/test-get-association-hasone.js renamed to test/integration/deprecated/test-get-association-hasone.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ common.createConnection(function (err, db) {
1313
TestModel.hasOne("assoc");
1414

1515
TestModel(1).getAssoc(function (err, Test2) {
16-
console.log(err, Test2);
1716
assert.equal(err, null);
1817
assert.equal(typeof Test2, "object");
1918
assert.equal(Test2.id, 2);

test/integration2/association-hasone.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ describe("hasOne", function() {
99
var Tree = null;
1010
var Stalk = null;
1111
var Leaf = null;
12+
var leafId = null;
13+
var treeId = null;
14+
var stalkId = null;
1215

1316
var setup = function (opts) {
1417
opts = opts || {};
@@ -28,12 +31,16 @@ describe("hasOne", function() {
2831
return helper.dropSync([Tree, Stalk, Leaf], function() {
2932
Tree.create({ type: 'pine' }, function (err, tree) {
3033
should.not.exist(err);
34+
treeId = tree.id;
3135
Leaf.create({ size: 14 }, function (err, leaf) {
3236
should.not.exist(err);
33-
leaf.setTree(tree, function () {
37+
leafId = leaf.id;
38+
leaf.setTree(tree, function (err) {
39+
should.not.exist(err);
3440
Stalk.create({ length: 20 }, function (err, stalk) {
3541
should.not.exist(err);
3642
should.exist(stalk);
43+
stalkId = stalk.id;
3744
done();
3845
});
3946
});
@@ -65,6 +72,15 @@ describe("hasOne", function() {
6572
});
6673
});
6774

75+
it("get should get the association with a shell model", function (done) {
76+
Leaf(leafId).getTree(function (err, tree) {
77+
should.not.exist(err);
78+
should.exist(tree);
79+
should.equal(tree.id, treeId);
80+
done();
81+
});
82+
});
83+
6884
it("has should indicate if there is an association present", function (done) {
6985
Leaf.one({ size: 14 }, function (err, leaf) {
7086
should.not.exist(err);

0 commit comments

Comments
 (0)