Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task
circulon opened this issue Aug 25, 2022 · 2 comments
Closed
1 task

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

circulon opened this issue Aug 25, 2022 · 2 comments
Labels
enhancement A feature that exists, works as intended but needs to be improved feature request A feature that does not yet exist but will be a good addition to the library

Comments

@circulon
Copy link
Contributor

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.

  • Is this a breaking change?
    No
@circulon circulon added enhancement A feature that exists, works as intended but needs to be improved feature request A feature that does not yet exist but will be a good addition to the library labels Aug 25, 2022
@circulon circulon changed the title Question: Different Relation Model base3d on Model attributes - how? Question: Different Relation Model based on Model attribute - how? Aug 25, 2022
@josephmancuso
Copy link
Member

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

@circulon
Copy link
Contributor Author

@josephmancuso

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

Thanks heaps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature that exists, works as intended but needs to be improved feature request A feature that does not yet exist but will be a good addition to the library
Projects
None yet
Development

No branches or pull requests

2 participants