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
165 stars 48 forks source link

Question: Different Relation Model based on Model attribute - how? #783

Closed circulon closed 2 years ago

circulon commented 2 years ago

Describe the feature as you'd like to see it I have a couple of models which contain a UUID/ID (not foreign key) and a flag which denotes which Destination Model the UUID/ID is attached to.

In other ORM's I have dropped in some logic which checks the flag and then uses the correct model. Currently this logic (in masonite orm) results in a recursion error as using self in the decorated method to check the attribute is not possible.
I'm not sure if this is a bug or a feature request or is this something that can be achieved via a lambda/function


class Vehicle(Model):
    @has_one("id", "vehicle_id")
    def vehicle(self):
        if self.vehicle_type == 'truck':
            return Truck

        if self.vehicle_type == 'car':
            return Car

In this example the recursion error is due to the self.vehicle_type in the method which calls the Model (self) to get its relations and attributes which then descends into the decorated vehicle method, which then gets the Model (self) relations ........

is there another way to achive this kind of `flagged' logic? I'm happy to try out new logic but this seems to be the most straight forward to achieve the outcome Using a pivot table is not really suitable for this scenario.

What do we currently have to do now? Don't have any way to do this currently

Additional context Add any other context or screenshots about the feature request here.

josephmancuso commented 2 years ago

You'll need to use polymorphic relationships https://orm.masoniteproject.com/models#polymorphic-relationships

circulon commented 2 years ago

@josephmancuso

This is exactly what I was hoping to find was already available.
I obviously missed this in the docs previously.

Thanks heaps.