orm
orm copied to clipboard
BaseRelationship: s/self/instance
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__
can you expand on what this is fixing?
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__