-
Notifications
You must be signed in to change notification settings - Fork 198
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
with ... as ... statement can make sesssion close,which will lead to DetachedInstanceError #776
Comments
Hey, thanks for reporting this.
|
I can confirm this. This can be easily reproduced if you try using any field from the relation in
|
Yes, I did reconstruct the method repr of models in my project. I was unaware that this was the issue. |
That is expected. The SQLAlchemy session is closed at this point so you cannot load any relationship that was not loaded. I can think of two options for this:
|
Pardon, I didn't understand the sentence "SQLAdmin should not relationships so they can be used in the repr or str methods." Could you please explain it? |
@ChengChe106 Sorry I wrote that in a hurry. I have edited my comment, let me know if you have any questions. |
I try the first option. It cann't work. If I add "User.permission_groups" to the "column_list" and "column_details_list" in here, like this:
This error is raised:
If I don't add "User.permission_groups" to column_list, users list page is good. But, no matter how, visiting users detail page will raise the error mentioned above. |
Please provide a minimum code to reproduce the issue. I see that you are using dataclasses but don't see how. |
Checklist
master
.Describe the bug
sqladmin/models
In the code, the use of a with ... as ... statement to manage the session led to an unexpected closure of the session, which may caused the following error in some case:
sqlalchemy.orm.exc.DetachedInstanceError:
This error occurs because closing the session detaches instances that still require the session for lazy loading or other operations.
To resolve this, I modified the code to manage the session explicitly, avoiding its premature closure. This change prevents the DetachedInstanceError by keeping the session open until all required operations are complete.
With this modification, the error no longer occurs, and the instances remain attached to the session as needed.
Steps to reproduce the bug
No response
Expected behavior
No response
Actual behavior
No response
Debugging material
No response
Environment
Windows 10 / ptyhon 3.10.11 / sqladmin 0.16.1
Additional context
No response
The text was updated successfully, but these errors were encountered: