orm icon indicating copy to clipboard operation
orm copied to clipboard

BaseRelationship: s/self/instance

Open pmn4 opened this issue 2 years ago • 2 comments

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__

pmn4 avatar Feb 12 '24 21:02 pmn4

can you expand on what this is fixing?

josephmancuso avatar Feb 12 '24 21:02 josephmancuso

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__

pmn4 avatar Feb 12 '24 21:02 pmn4