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
SQLModel does not store relationships data directly in attributes, so trying to use them like vanilla SQLAlchemy attributes like that
class ModelA(SQLModel, table=True):
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True, nullable=False)
class ModelB(SQLModel, table=True):
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True, nullable=False)
model_a: Optional[ModelA] = Relationship(sa_relationship=relationship("ModelA"))
class ModelBAdmin(ModelAdmin, model=ModelB):
column_list = [ModelB.model_a]
results in error:
AttributeError: type object 'ModelB' has no attribute 'model_a'
Trying to use attribute used by SQLModel does not work either:
class ModelBAdmin(ModelAdmin, model=ModelB):
column_list = [ModelB.__sqlmodel_relationships__["model_a"].sa_relationship]
error:
File "/usr/local/lib/python3.9/site-packages/sqladmin/models.py", line 590, in get_model_attr
assert isinstance(attr, (str, InstrumentedAttribute))
AssertionError
since what is checked in this assertion is sqlalchemy.orm.RelationshipProperty
I tried to work around that making some patch:
from typing import Union
from sqladmin.models import ModelAdmin
from sqlalchemy.orm import InstrumentedAttribute, RelationshipProperty, ColumnProperty
class ModelAdminWithSQLModelRelationships(ModelAdmin):
def get_model_attr(
self, attr: Union[str, InstrumentedAttribute, RelationshipProperty]
) -> Union[ColumnProperty, RelationshipProperty]:
if isinstance(attr, RelationshipProperty):
return attr
return super().get_model_attr(attr)
But it didn't work either
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 1214, in _fallback_getattr
raise AttributeError(key)
AttributeError: key
so I've added some utitlity function to workaround that:
def get_sqlmodel_relationship(model, name):
rel = model.__sqlmodel_relationships__[name].sa_relationship
rel.key = name
return rel
class ModelBAdmin(ModelAdminWithSQLModelRelationships, model=ModelB):
column_list = [get_sqlmodel_relationship(ModelB, "model_a")]
At this point app could complete startup and I could access admin panel, but when I tried to access list of ModelB objects exception was raised:
sqlalchemy.exc.ArgumentError: Can't find property named "model_a" on mapped class ModelB->modelb in this Query.
so, at this point I gave up, it seems that it would need much more monkeypatching than I anticipated
Steps to reproduce the bug
No response
Expected behavior
Possibility to use SQLModel Relationship in admin panel just like in vanilla SQLAlchemy
Actual behavior
No response
Debugging material
No response
Environment
OS: Manjaro
Python: 3.9.7
SQLAlchemy: 1.4.36
Additional context
No response
The text was updated successfully, but these errors were encountered:
Checklist
master
.Describe the bug
SQLModel does not store relationships data directly in attributes, so trying to use them like vanilla SQLAlchemy attributes like that
results in error:
Trying to use attribute used by SQLModel does not work either:
error:
since what is checked in this assertion is
sqlalchemy.orm.RelationshipProperty
I tried to work around that making some patch:
But it didn't work either
so I've added some utitlity function to workaround that:
At this point app could complete startup and I could access admin panel, but when I tried to access list of
ModelB
objects exception was raised:so, at this point I gave up, it seems that it would need much more monkeypatching than I anticipated
Steps to reproduce the bug
No response
Expected behavior
Possibility to use SQLModel Relationship in admin panel just like in vanilla SQLAlchemy
Actual behavior
No response
Debugging material
No response
Environment
OS: Manjaro
Python: 3.9.7
SQLAlchemy: 1.4.36
Additional context
No response
The text was updated successfully, but these errors were encountered: