Skip to content

Support MappedAsDataclass#857

Merged
mmzeynalli merged 5 commits intosmithyhq:mainfrom
Goradii:support-mapped-as-dataclass
Apr 18, 2026
Merged

Support MappedAsDataclass#857
mmzeynalli merged 5 commits intosmithyhq:mainfrom
Goradii:support-mapped-as-dataclass

Conversation

@Goradii
Copy link
Copy Markdown
Contributor

@Goradii Goradii commented Nov 19, 2024

fixed insertion error if MappedAsDataclass #737

BEFORE FIX 3.8

FAILED tests/test_sa2/test_view_sync.py::test_sync_create_dataclass - assert 0 == 1
FAILED tests/test_sa2/test_views_async.py::test_async_create_dataclass - assert 0 == 1
2 failed, 179 passed, 3 skipped, 7 warnings in 40.15s

AFTER FIX 3.8

181 passed, 3 skipped, 7 warnings in 41.22s

@mmzeynalli
Copy link
Copy Markdown
Member

@Goradii can you check if latest version of sqladmin still have this problem? If yes, let me know

@Goradii
Copy link
Copy Markdown
Contributor Author

Goradii commented Nov 26, 2025

Yes same issue 😞

py313

$ uv tree | grep sqladmin
Resolved 83 packages in 2ms
├── sqladmin v0.22.0

@mmzeynalli mmzeynalli linked an issue Apr 6, 2026 that may be closed by this pull request
2 tasks
Comment thread sqladmin/_queries.py Outdated
Comment on lines +192 to +203
def _prepare_insert_dataclass(self, data: dict[str, Any]) -> dict[str, Any]:
try:
init = {
k: v
for k, v in data.items()
if self.model_view.model.__dataclass_fields__[k].init # type: ignore[attr-defined] # caught in except block
}
except AttributeError:
return {}
else:
return init

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can easily check with dataclasses.is_dataclass

Comment thread sqladmin/_queries.py Outdated
Comment on lines 205 to 206
obj = self.model_view.model(**self._prepare_insert_dataclass(data))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead update here and in _insert_async:

if dataclasses.is_dataclass(self.model_view.model):
    init_fields = {f.name for f in dataclasses.fields(self.model_view.model) if f.init}
    obj = self.model_view.model(**{k: v for k, v in data.items() if k in init_fields})
else:
    obj = self.model_view.model()

@mmzeynalli mmzeynalli added the waiting-for-feedback Waiting feedback/answer/updates from contributor label Apr 8, 2026
@mmzeynalli mmzeynalli closed this Apr 11, 2026
@mmzeynalli mmzeynalli added the stale This feature/issue has no updates by contributor. Will be closed soon. label Apr 11, 2026
@mmzeynalli mmzeynalli reopened this Apr 11, 2026
@mmzeynalli mmzeynalli added ready-to-merge Approved and ready to merge. Will be published with the next release. and removed waiting-for-feedback Waiting feedback/answer/updates from contributor stale This feature/issue has no updates by contributor. Will be closed soon. labels Apr 12, 2026
@mmzeynalli mmzeynalli merged commit a403bcf into smithyhq:main Apr 18, 2026
12 checks passed
@mmzeynalli mmzeynalli mentioned this pull request Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Approved and ready to merge. Will be published with the next release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqlAlchemy dataclass support missing

2 participants