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

Collection returns empty object for null relations #1771

Closed
pbassut opened this issue Feb 22, 2018 · 6 comments · Fixed by #1839
Closed

Collection returns empty object for null relations #1771

pbassut opened this issue Feb 22, 2018 · 6 comments · Fixed by #1839
Assignees

Comments

@pbassut
Copy link

pbassut commented Feb 22, 2018

Introduction

collection with hasOne relation returns empty objects instead of null

Issue Description

If you have a collection with a hasOne relation and you fetch that relation with withRelated on fetchAll the relations that come out to be null are not null but have an empty object instead.

Steps to reproduce issue

Suppose you have a model like:

// User.js
module.exports = {
    tableName: 'user',
    location() {
        return this.hasOne('user_location', 'user_id');
    },
}

And you do: User.fetchAll({ withRelated: ['location'] }). What you'll get is:

[
{ id: 1, location: { id: 1, name: 'my house' } },
{ id: 2, location: { id: 2, name: 'office' } },
{ id: 3, location: {} },
]

Expected behaviour

location should be null instead of a truthy object like with belongsTo.

Actual behaviour

location are empty objects which makes it difficult to do if(user.location).

@nabilfreeman
Copy link

nabilfreeman commented Apr 3, 2018

+1 - we are getting around it by checking relation && relation.get('id').

Bookshelf has really come on in leaps and bounds over the past 18 months, so I know it's only a matter of time before this is fixed 🤗

@ricardograca ricardograca self-assigned this May 9, 2018
@ricardograca ricardograca moved this from To Do to In progress in Version 0.14.0 May 9, 2018
Version 0.14.0 automation moved this from In progress to Done May 10, 2018
@cacharrin
Copy link

cacharrin commented May 14, 2019

Im using version 14.2 and im having this exact problem...
I have a relation with hasOne... loaded through

Process.where({
      id: id
    }).fetch({
        withRelated: [ {
            "activities.userActivityPoints": q => {
              q.where({ productivity_user_id: user.get("id") });
           }}
        ]
      });

And that is returning for users with related data (Its OK):

{
        "id": 1,
        "process_id": 1,
        "description": "activity1-1-1",
        "userActivityPoints": {
            "points": 50,
            "frequency": {
                "id": 2,
                "name": "Semanal"
            }
        }
    },

But is returning empty obejct { } for users with no related data

{ 
    "id": 1,
    "activities": [
        {
            "id": 2,
            "process_id": 1,
            "description": "activity2-1-1",
            "userActivityPoints": {}
        }
    ]
}

Any ideas?

@douglasjunior
Copy link

douglasjunior commented Feb 27, 2020

Any updates?

I also reported this problem on #1299 (comment).

@ricardograca
Copy link
Member

How can there be any updates if you just reported the issue?

Can you create a new issue explaining the problem with steps to reproduce?

@douglasjunior
Copy link

douglasjunior commented Feb 28, 2020

Thanks for the quick response. It's not just me, the @cacharrin also reported the same problem after #1299 was fixed.

I will try to create a small sample project.

@durai001
Copy link

Also reported this kind of issue on #2090. I have researched many times it not resolved. please tell me if anyone knows, how to solve it.

to achieve this I used knex but need full code to restructure. But I believe there is some way in bookshlef to do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

6 participants