You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
My Stack:
FastAPI
SqlModel
SqlAdmin
PostgreSQL 12
If I have a SqlAdmin ModelView for a SqlModel model with many relationships defined, the query used to populate the details view and the edit view does a join on all relations even when the related fields are not included in the ModelView's column_details_list or form_columns.
This is an issue for me because one of my models has a many-to-many relationship with another table that can include 10's of thousands of records in the relationship. The model has other relations as well, but those have fewer records. When attempting to load the details or edit view for this model the request never completes because the join is so large and I need to restart the server.
I suspect this issue may be related to either SqlModel or SqlAlchemy internal implementation details/behavior. i.e. they are joining all relationships by default. However, I did see there was a fix released for SqlAdmin that fixed a similar issue for the list view here: #409
Steps to reproduce the bug
Please note that this minimal example will likely not reproduce the issue because it only occurs due to the high number of related records in my database. Brand is the model I'm trying to load the detail and edit view for. Asset is the related model with 10's of thousands of related records.
I would expect the detail and edit views to only perform joins for the relationships defined in the column_details_list and form_columns attributes.
Actual behavior
Detail and edit views perform joins for ALL related fields even when not specified in column_details_list and form_columns.
Debugging material
No response
Environment
Mac
application is running in a docker container: python:3.8-slim
Connected to external Postgres Db
Additional context
I also tried putting the related fields in the ModelView's form_ajax_refs attribute to see if that would defer the joins, but that didn't seem to change anything.
The text was updated successfully, but these errors were encountered:
Hey @FFX01
You are correct about a similar fix that was done and this issue.
This is because there's only one shared method get_model_by_pk that is causing this:
The reason for that is that this method is called from different places and can't respect the relationships specified, I think a simple solution can be to separate this method for different purposes. Should be an easy fix, feel free to do a PR otherwise I will fix it soon.
Checklist
master
.Describe the bug
My Stack:
If I have a SqlAdmin
ModelView
for a SqlModel model with many relationships defined, the query used to populate the details view and the edit view does a join on all relations even when the related fields are not included in theModelView
'scolumn_details_list
orform_columns
.This is an issue for me because one of my models has a many-to-many relationship with another table that can include 10's of thousands of records in the relationship. The model has other relations as well, but those have fewer records. When attempting to load the details or edit view for this model the request never completes because the join is so large and I need to restart the server.
I suspect this issue may be related to either SqlModel or SqlAlchemy internal implementation details/behavior. i.e. they are joining all relationships by default. However, I did see there was a fix released for SqlAdmin that fixed a similar issue for the list view here: #409
Steps to reproduce the bug
Please note that this minimal example will likely not reproduce the issue because it only occurs due to the high number of related records in my database.
Brand
is the model I'm trying to load the detail and edit view for.Asset
is the related model with 10's of thousands of related records.Expected behavior
I would expect the detail and edit views to only perform joins for the relationships defined in the
column_details_list
andform_columns
attributes.Actual behavior
Detail and edit views perform joins for ALL related fields even when not specified in
column_details_list
andform_columns
.Debugging material
No response
Environment
python:3.8-slim
Additional context
I also tried putting the related fields in the
ModelView
'sform_ajax_refs
attribute to see if that would defer the joins, but that didn't seem to change anything.The text was updated successfully, but these errors were encountered: