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
160 stars 47 forks source link

BaseRelationship: s/self/instance #869

Closed pmn4 closed 3 months ago

pmn4 commented 4 months ago

when migrating from Orator to Masonite, several of my relationship methods were not working. Stepping through the code, I found that when referencing the class via self.__class__ inside the User class, self was an instance of BelongsTo, not User. This PR aims to pass an instance of User instead.

class User(Model):

    @belongs_to
    def parent(self):
        # ⚠️ self is an instance of BelongsTo ⚠️ 
        # expecting self to be an instance of User
        return self.__class__
josephmancuso commented 4 months ago

can you expand on what this is fixing?

pmn4 commented 4 months ago

oops, this isn't a good example. the user method should return the User class. I tried to make my example simpler, and instead made it confusing! instead, consider a User class that has a parent relationship, where parent is another record in the User table. you could say:

@belong_to 
def parent(self):
    return User

or

@belong_to 
def parent(self):
    return self.__class__