Skip to content

Commit b704d31

Browse files
committed
Merge pull request #681 from stueynz/hasOne-with-zeroID
Has one with zero
2 parents 2f4a2d3 + 715c029 commit b704d31

File tree

5 files changed

+161
-6
lines changed

5 files changed

+161
-6
lines changed

lib/Drivers/DML/postgres.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,10 @@ Driver.prototype.insert = function (table, data, keyProperties, cb) {
213213
if (keyProperties) {
214214
for (i = 0; i < keyProperties.length; i++) {
215215
prop = keyProperties[i];
216-
217-
ids[prop.name] = results[0][prop.mapsTo] || null;
216+
// Zero is a valid value for an ID column
217+
ids[prop.name] = results[0][prop.mapsTo] !== undefined ? results[0][prop.mapsTo] : null;
218218
}
219219
}
220-
221220
return cb(null, ids);
222221
});
223222
};

lib/Drivers/DML/redshift.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Driver.prototype.insert = function (table, data, keyProperties, cb) {
3434
} else {
3535
for(i = 0; i < keyProperties.length; i++) {
3636
prop = keyProperties[i];
37-
ids[prop.name] = data[prop.mapsTo] || null;
37+
// Zero is a valid value for an ID column
38+
ids[prop.name] = data[prop.mapsTo] !== undefined ? data[prop.mapsTo] : null;
3839
}
3940

4041
return cb(null, ids);

lib/Drivers/DML/sqlite.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ Driver.prototype.insert = function (table, data, keyProperties, cb) {
175175
} else {
176176
for (i = 0; i < keyProperties.length; i++) {
177177
prop = keyProperties[i];
178-
ids[prop.name] = data[prop.mapsTo] || null;
178+
// Zero is a valid value for an ID column
179+
ids[prop.name] = data[prop.mapsTo] !== undefined ? data[prop.mapsTo] : null;
179180
}
180181
return cb(null, ids);
181182
}

lib/Utilities.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,12 @@ exports.values = function (obj, keys) {
131131
return vals;
132132
};
133133

134+
// Qn: is Zero a valid value for a FK column?
135+
// Why? Well I've got a pre-existing database that started all its 'serial' IDs at zero...
136+
// Answer: hasValues() is only used in hasOne association, so it's probably ok...
134137
exports.hasValues = function (obj, keys) {
135138
for (var i = 0; i < keys.length; i++) {
136-
if (!obj[keys[i]]) return false;
139+
if (!obj[keys[i]] && obj[keys[i]] !== 0) return false; // 0 is also a good value...
137140
}
138141
return true;
139142
};
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
var ORM = require('../../');
2+
var helper = require('../support/spec_helper');
3+
var should = require('should');
4+
var async = require('async');
5+
var _ = require('lodash');
6+
7+
describe("hasOne", function() {
8+
var db = null;
9+
var Person = null;
10+
var Pet = null;
11+
12+
var setup = function (autoFetch) {
13+
return function (done) {
14+
db.settings.set('instance.cache', false);
15+
db.settings.set('instance.returnAllErrors', true);
16+
17+
Person = db.define('person', {
18+
id : {type : "integer", key:true},
19+
firstName : {type : "text", size:"255"},
20+
lastName : {type : "text", size:"255"}
21+
});
22+
23+
Pet = db.define('pet', {
24+
id : {type : "integer", key:true},
25+
petName : {type : "text", size:"255"},
26+
ownerID : {type : "integer", size:"4"}
27+
});
28+
29+
Pet.hasOne('owner', Person, {field: 'ownerID', autoFetch:autoFetch});
30+
31+
helper.dropSync([Person, Pet], function(err) {
32+
if (err) return done(err);
33+
Pet.create([{
34+
id: 10,
35+
petName: 'Muttley',
36+
owner: {
37+
id: 12,
38+
firstName: 'Stuey',
39+
lastName: 'McG'
40+
}
41+
},
42+
{
43+
id: 11,
44+
petName: 'Snagglepuss',
45+
owner: {
46+
id: 0,
47+
firstName: 'John',
48+
lastName: 'Doe'
49+
}
50+
}], done);
51+
});
52+
};
53+
};
54+
55+
before(function(done) {
56+
helper.connect(function (connection) {
57+
db = connection;
58+
done();
59+
});
60+
});
61+
62+
describe("auto fetch", function () {
63+
before(setup(true));
64+
65+
it("should work for non-zero ownerID ", function (done) {
66+
Pet.find({petName: "Muttley"}, function(err, pets) {
67+
should.not.exist(err);
68+
69+
pets[0].petName.should.equal("Muttley");
70+
pets[0].should.have.property("id");
71+
pets[0].id.should.equal(10);
72+
pets[0].ownerID.should.equal(12);
73+
74+
pets[0].should.have.property("owner");
75+
pets[0].owner.firstName.should.equal("Stuey");
76+
77+
return done();
78+
});
79+
});
80+
81+
it("should work for zero ownerID ", function (done) {
82+
Pet.find({petName: "Snagglepuss"}, function(err, pets) {
83+
should.not.exist(err);
84+
85+
pets[0].petName.should.equal("Snagglepuss");
86+
pets[0].should.have.property("id");
87+
pets[0].id.should.equal(11);
88+
pets[0].ownerID.should.equal(0);
89+
90+
pets[0].should.have.property("owner");
91+
pets[0].owner.firstName.should.equal("John");
92+
93+
return done();
94+
});
95+
});
96+
});
97+
98+
describe("no auto fetch", function () {
99+
before(setup(false));
100+
101+
it("should work for non-zero ownerID ", function (done) {
102+
Pet.find({petName: "Muttley"}, function(err, pets) {
103+
should.not.exist(err);
104+
105+
pets[0].petName.should.equal("Muttley");
106+
pets[0].should.have.property("id");
107+
pets[0].id.should.equal(10);
108+
pets[0].ownerID.should.equal(12);
109+
110+
pets[0].should.not.have.property("owner");
111+
112+
// But we should be able to see if its there
113+
pets[0].hasOwner(function(err, result) {
114+
should.equal(result, true);
115+
});
116+
117+
// ...and then get it
118+
pets[0].getOwner(function(err, result) {
119+
result.firstName.should.equal("Stuey");
120+
});
121+
122+
return done();
123+
});
124+
});
125+
126+
it("should work for zero ownerID ", function (done) {
127+
Pet.find({petName: "Snagglepuss"}, function(err, pets) {
128+
should.not.exist(err);
129+
130+
pets[0].petName.should.equal("Snagglepuss");
131+
pets[0].should.have.property("id");
132+
pets[0].id.should.equal(11);
133+
pets[0].ownerID.should.equal(0);
134+
135+
pets[0].should.not.have.property("owner");
136+
137+
// But we should be able to see if its there
138+
pets[0].hasOwner(function(err, result) {
139+
should.equal(result, true);
140+
});
141+
142+
// ...and then get it
143+
pets[0].getOwner(function(err, result) {
144+
result.firstName.should.equal("John");
145+
});
146+
147+
return done();
148+
});
149+
});
150+
});
151+
});

0 commit comments

Comments
 (0)