Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Has one with zero #681

Merged
merged 6 commits into from
Dec 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/Drivers/DML/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,10 @@ Driver.prototype.insert = function (table, data, keyProperties, cb) {
if (keyProperties) {
for (i = 0; i < keyProperties.length; i++) {
prop = keyProperties[i];

ids[prop.name] = results[0][prop.mapsTo] || null;
// Zero is a valid value for an ID column
ids[prop.name] = results[0][prop.mapsTo] !== undefined ? results[0][prop.mapsTo] : null;
}
}

return cb(null, ids);
});
};
Expand Down
3 changes: 2 additions & 1 deletion lib/Drivers/DML/redshift.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Driver.prototype.insert = function (table, data, keyProperties, cb) {
} else {
for(i = 0; i < keyProperties.length; i++) {
prop = keyProperties[i];
ids[prop.name] = data[prop.mapsTo] || null;
// Zero is a valid value for an ID column
ids[prop.name] = data[prop.mapsTo] !== undefined ? data[prop.mapsTo] : null;
}

return cb(null, ids);
Expand Down
3 changes: 2 additions & 1 deletion lib/Drivers/DML/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ Driver.prototype.insert = function (table, data, keyProperties, cb) {
} else {
for (i = 0; i < keyProperties.length; i++) {
prop = keyProperties[i];
ids[prop.name] = data[prop.mapsTo] || null;
// Zero is a valid value for an ID column
ids[prop.name] = data[prop.mapsTo] !== undefined ? data[prop.mapsTo] : null;
}
return cb(null, ids);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ exports.values = function (obj, keys) {
return vals;
};

// Qn: is Zero a valid value for a FK column?
// Why? Well I've got a pre-existing database that started all its 'serial' IDs at zero...
// Answer: hasValues() is only used in hasOne association, so it's probably ok...
exports.hasValues = function (obj, keys) {
for (var i = 0; i < keys.length; i++) {
if (!obj[keys[i]]) return false;
if (!obj[keys[i]] && obj[keys[i]] !== 0) return false; // 0 is also a good value...
}
return true;
};
Expand Down
151 changes: 151 additions & 0 deletions test/integration/association-hasone-zeroid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
var ORM = require('../../');
var helper = require('../support/spec_helper');
var should = require('should');
var async = require('async');
var _ = require('lodash');

describe("hasOne", function() {
var db = null;
var Person = null;
var Pet = null;

var setup = function (autoFetch) {
return function (done) {
db.settings.set('instance.cache', false);
db.settings.set('instance.returnAllErrors', true);

Person = db.define('person', {
id : {type : "integer", key:true},
firstName : {type : "text", size:"255"},
lastName : {type : "text", size:"255"}
});

Pet = db.define('pet', {
id : {type : "integer", key:true},
petName : {type : "text", size:"255"},
ownerID : {type : "integer", size:"4"}
});

Pet.hasOne('owner', Person, {field: 'ownerID', autoFetch:autoFetch});

helper.dropSync([Person, Pet], function(err) {
if (err) return done(err);
Pet.create([{
id: 10,
petName: 'Muttley',
owner: {
id: 12,
firstName: 'Stuey',
lastName: 'McG'
}
},
{
id: 11,
petName: 'Snagglepuss',
owner: {
id: 0,
firstName: 'John',
lastName: 'Doe'
}
}], done);
});
};
};

before(function(done) {
helper.connect(function (connection) {
db = connection;
done();
});
});

describe("auto fetch", function () {
before(setup(true));

it("should work for non-zero ownerID ", function (done) {
Pet.find({petName: "Muttley"}, function(err, pets) {
should.not.exist(err);

pets[0].petName.should.equal("Muttley");
pets[0].should.have.property("id");
pets[0].id.should.equal(10);
pets[0].ownerID.should.equal(12);

pets[0].should.have.property("owner");
pets[0].owner.firstName.should.equal("Stuey");

return done();
});
});

it("should work for zero ownerID ", function (done) {
Pet.find({petName: "Snagglepuss"}, function(err, pets) {
should.not.exist(err);

pets[0].petName.should.equal("Snagglepuss");
pets[0].should.have.property("id");
pets[0].id.should.equal(11);
pets[0].ownerID.should.equal(0);

pets[0].should.have.property("owner");
pets[0].owner.firstName.should.equal("John");

return done();
});
});
});

describe("no auto fetch", function () {
before(setup(false));

it("should work for non-zero ownerID ", function (done) {
Pet.find({petName: "Muttley"}, function(err, pets) {
should.not.exist(err);

pets[0].petName.should.equal("Muttley");
pets[0].should.have.property("id");
pets[0].id.should.equal(10);
pets[0].ownerID.should.equal(12);

pets[0].should.not.have.property("owner");

// But we should be able to see if its there
pets[0].hasOwner(function(err, result) {
should.equal(result, true);
});

// ...and then get it
pets[0].getOwner(function(err, result) {
result.firstName.should.equal("Stuey");
});

return done();
});
});

it("should work for zero ownerID ", function (done) {
Pet.find({petName: "Snagglepuss"}, function(err, pets) {
should.not.exist(err);

pets[0].petName.should.equal("Snagglepuss");
pets[0].should.have.property("id");
pets[0].id.should.equal(11);
pets[0].ownerID.should.equal(0);

pets[0].should.not.have.property("owner");

// But we should be able to see if its there
pets[0].hasOwner(function(err, result) {
should.equal(result, true);
});

// ...and then get it
pets[0].getOwner(function(err, result) {
result.firstName.should.equal("John");
});

return done();
});
});
});
});