question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Related models are empty objects instead of null when not found

See original GitHub issue

I don’t know if there is a practical reason for this, but when using model.fetch({ withRelated: ['otherModel'] }), the results for rows that did not have an otherModel relation have the property otherModel set to {}, an empty object. Shouldn’t this field be set to null? And if not, why is an empty object the preferred representation of a non-existent relation over false, null or undefined?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Nick-Riggscommented, Mar 6, 2017

We’ve also encountered this. Here are some of my findings:

  1. This only occurs when using the pagination plugin’s fetchPage method. fetch() works as expected.
  2. This is a performance issue as fetchPage isn’t just attaching a empty object for the relation, it’s actually firing the queries against the database looking for null value relations. fetch() does not do this.
0reactions
douglasjuniorcommented, Feb 28, 2020

This problem still persists in 1.0.0, 1.0.1 e 1.1.0, with fetchPage.

For example: If in a list one record has a relation, all others records has an empty property.

[
    {
        "name": "Douglas",
        "address": {
            "stree": "One"
        }
    },
    {
        "name": "Bruna",
        "address": { }
    },
    {
        "name": "Bel",
        "address": { }
    }
]

If no one has a relationship, then the relation property is omitted.

[
    {
        "name": "Bruna",
    },
    {
        "name": "Bel",
    }
]

cc @edvaldoszy

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to show empty object instead of NULL when the one To ...
Hello , in my controller I return users with 'groups' and 'sidnfo' models . the module 'sdinfo' in one to one relationship, public...
Read more >
Gorm return empty object instead default object when value is ...
GORM is returning an empty object; when it comes to Go values, "empty" and "default" are the same, and are actually called the...
Read more >
Filtering for Empty or Null Values in a Django QuerySet - Chartio
Easily the most important method when working with Django models and the underlying QuerySets is the filter() method, which allows you to generate...
Read more >
How to return a empty object when linq query yields nothing ...
I'm trying to query the database using linq query and the FirstOrDefault yields null value. Is there a way I can return a...
Read more >
When to use NULL and when to use an empty string?
It does. Now looking at DBMS where '' is not identical to NULL (e.g. SQL-Server). Working with '' is generally easier and in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found