MasoniteFramework / orm

Masonite ORM is a beautiful Python ORM. It's also a nearly drop in replacement of the Orator ORM
https://orm.masoniteproject.com
MIT License
177 stars 49 forks source link

One-to-many relationships are serialized with None value when eager-loaded #798

Open federicoparroni opened 2 years ago

federicoparroni commented 2 years ago

Describe the bug When one-to-many relationships is eager-loaded (using the _with function) from a record, the serialized value for the related field is set as None.

To Reproduce

class Category(Model):
    __hidden__ = [
        'id',
        'created_at',
        'updated_at'
    ]

    @belongs_to('website_id', 'id')
    def websites(self):
        return Website

class Website(Model, SoftDeletesMixin):
    __appends__ = [
        'categories',
    ]

    @has_many('id', 'website_id')
    def categories(self):
       return Category

data = Website.with_('categories').first().serialize()
print(data)    # data["categories"] is None

Expected behavior The serialized value for the related field should be set as an empty array.

Desktop (please complete the following information):

What database are you using?

Chiu-Kevin-bah commented 1 year ago

Also take out the appends. That’s for custom attributes. You have the with() already