Skip to content

Commit

Permalink
allow relationship functions to return undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
refractalize committed Mar 10, 2016
1 parent 7abdf74 commit 13c1bc5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var rowBase = function() {
function saveManyToOne(obj, field) {
var value = foreignField(obj, field);

if (!(value instanceof Array)) {
if (value && !(value instanceof Array)) {
return value.save().then(function () {
var foreignId =
obj._meta.foreignKeyFor ?
Expand Down
26 changes: 26 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,32 @@ function describeDatabase(name, config) {
});
});

it("can save a many to one relationship with function that returns undefined", function() {
var bobsAddress;
var bob = person({
name: "bob",
address: function () {
}
});

return bob.save().then(function() {
expect(statements).to.eql([ "insert" ]);
expect(bob.address).to.equal(bobsAddress);

return db.query("select * from addresses").then(function(addresses) {
expect(helpers.clean(addresses)).to.eql([]);
});
}).then(function () {
return db.query("select * from people").then(function(people) {
expect(helpers.clean(people)).to.eql([{
id: bob.id,
name: 'bob',
address_id: null
}]);
});
});
});

describe("custom foreign keys", function() {
it("can save a many to one relationship with a custom foreign key", function() {
var personWeirdId = db.model({
Expand Down

0 comments on commit 13c1bc5

Please sign in to comment.