Skip to content

Commit

Permalink
Foreign key is nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
tdesvenain committed Mar 21, 2018
1 parent b53c131 commit c0ee612
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const Relationships = (db) => {
rows.forEach(row => {
let foreignKey = row[targetIndex]
let record = lookup[foreignKey]
if (!record) {
if (foreignKey !== null && foreignKey !== undefined && !record) {
throw new Error(
`Could not lookup foreign key where ` +
`${tableName}.${foreignIndex} == ${baseTable}.${column}. ` +
Expand Down
25 changes: 19 additions & 6 deletions test/browser/all-browser-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ describe('simple', function () {
name: 'Waterloo',
bandId: 2,
year: 1974
}, {
id: 5,
name: 'No band',
bandId: null,
year: 1974
}])
})
})
Expand All @@ -91,16 +96,20 @@ describe('simple', function () {
return db.albums.where('year').between(1970, 1974, true, true).with ({
band: 'bandId'
}).then (albums => {
assert (albums.length === 2, "Should retrieve two albums between 1970 to 1974")
assert (albums.length === 3, "Should retrieve three albums between 1970 to 1974")
let letItBe = albums[0],
waterloo = albums[1]
waterloo = albums[1],
noBand = albums[2];
assert (letItBe.name === "Let It Be", "First album should be 'Let It Be'")
assert (!!letItBe.band, "Should get the band resolved with the query")
assert (letItBe.band.name === "Beatles", "The band should be Beatles")

assert (waterloo.name === "Waterloo", "Second album should be 'Waterloo'")
assert (!!waterloo.band, "Should get the band resolved with the query")
assert (waterloo.band.name === "Abba", "The band should be Abba")

assert (noBand.name === "No band")
assert (!noBand.band, null)
})
})
})
Expand All @@ -110,13 +119,17 @@ describe('simple', function () {
return db.albums.with ({
band: 'bandId'
}).then (albums => {
assert (albums.length === 4, "Should retrieve four albums")
assert (albums.length === 5, "Should retrieve five albums")

let bandIds = [1, 1, 2, 2];
let bandIds = [1, 1, 2, 2, null];

albums.map((album, idx) => {
assert(album.band != null, "Each album should have a band")
assert(album.band.id === bandIds[idx], "Each album should be assigned the correct band")
if (bandIds[idx] !== null) {
assert(album.band != null, "Album should have a band")
assert(album.band.id === bandIds[idx], "Each album should be assigned the correct band")
} else {
assert(!album.band, "Album should not have a band")
}
})
})
})
Expand Down

0 comments on commit c0ee612

Please sign in to comment.