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

Handling of lazy="dynamic" relationships #388

Open
2 tasks done
jarojasm95 opened this issue Nov 28, 2022 · 5 comments
Open
2 tasks done

Handling of lazy="dynamic" relationships #388

jarojasm95 opened this issue Nov 28, 2022 · 5 comments

Comments

@jarojasm95
Copy link

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

Model relations are automatically loaded regardless of the ModelView configuration, this causes issues with relationships defined as lazy="dynamic" because it ends up in InvalidRequestError( '%s' does not support object population - eager loading cannot be applied.) from sqlalchemy. The error is raised on any list or detail request.

The current workaround I'm applying is:

class ModelView(ModelView):
    def __init__(self) -> None:
        super().__init__()
        self._relations = [rel for rel in self._relations if rel.lazy != "dynamic"]

Steps to reproduce the bug

Setting up two SQLAlchemy Models and configuring a relationship between them with the lazy="dynamic" parameter should suffice

Expected behavior

  1. Relationships not being listed as part of column_list or column_details_list are not eagerly loaded
  2. Relationships with lazy="dynamic" are either ignored (short term fix?) or correctly handled (longer term feature?)

Actual behavior

InvalidRequestError causes the ModelView to be unusable for models with lazy="dynamic" relationships

Debugging material

Starlette Debugger.pdf

Environment

  • OS: Debian 11 (Bullseye)
  • Python: 3.11
  • SQLAlchemy: 1.4.44

Additional context

No response

@jarojasm95
Copy link
Author

I see this other issue explains this might not be handled soon, maybe this could be added to docs? maybe the workaround shown here can also be helpful for others 🤷

@aminalaee
Copy link
Owner

Hey @jarojasm95 ,
I think it might even be better to apply this change to automatically ignore the unhandled models.
As for the long term I'm still not sure how that would look like with AsyncIO extension: https://docs.sqlalchemy.org/en/14/orm/collections.html#dynamic-relationship

@jarojasm95
Copy link
Author

@aminalaee thanks for the quick reply!

I'd be open to putting up that PR if that sounds good to you

@aminalaee
Copy link
Owner

Yes, that would be great.

@JakNowy
Copy link

JakNowy commented Mar 11, 2024

Bumping. Have this been adressed? I seem to have the same issue.

All relations are loaded from database regardless if they are displayed in list view or not.
Also
The unique() method must be invoked on this Result, as it contains results that include joined eager loads against collections
error pops upon trying to edit or delete an object which has nested joins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants