Skip to content

Fix foreign key = 0 not fetching related object - #1639

Merged
ricardograca merged 2 commits into
bookshelf:masterfrom
ollija:master
Jan 7, 2018
Merged

ricardograca merged 2 commits into
bookshelf:masterfrom
ollija:master

Conversation

@ollija

@ollija ollija commented Sep 4, 2017 •

Copy link
Copy Markdown
Contributor

Fix foreign key = 0 not fetching related object

Introduction

Fixes fetching relations with foreign key = 0.
Currently inside eagerPair-method the foreign keys are just checked for being falsy,
leaving the related instance out of the parent's relations if the FK is 0.

Fixes #1685.

Motivation

This PR enables fetching relations with value 0 as the foreign key.

Proposed solution

Compare the groupedKey-variable (which stores the foreign key) through lodashes isNil-method inside the if-block, instead of just checking for a falsy value. This way 0 is a valid value, but null / undefined values are not.

Comment thread src/relation.js Outdated
groupedKey = formatted[keyColumn];
}
if (groupedKey) {
if (groupedKey !== null) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any possibility of groupedKey ever being undefined?

@ricardograca

Copy link
Copy Markdown
Member

@ollija Can you add some tests to check for this? I'm asking because there was already a much earlier issue exactly like this one that was deemed fixed (#49), but it must have regressed at some point. I would like to prevent that from happening again in the future.

@ricardograca

Copy link
Copy Markdown
Member

I just added a test for this in #1735.

@ricardograca
ricardograca merged commit 83b6f65 into bookshelf:master Jan 7, 2018
ricardograca referenced this pull request Jan 7, 2018
Add test to check ability to eager load with foreign key with value 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants