Skip to content

feat: add SQLModel compatibility#686

Merged
cofin merged 9 commits intolitestar-org:mainfrom
cofin:feat/sqlmodel
Mar 8, 2026
Merged

feat: add SQLModel compatibility#686
cofin merged 9 commits intolitestar-org:mainfrom
cofin:feat/sqlmodel

Conversation

@cofin
Copy link
Copy Markdown
Member

@cofin cofin commented Mar 3, 2026

Summary

  • SQLModel table=True models now work seamlessly with Advanced Alchemy repositories and services without requiring AA base classes
  • ModelProtocol no longer requires to_dict() — models only need __mapper__, __table__, and __name__ (which all SQLAlchemy-mapped models have)
  • New model_to_dict() utility converts any mapped model to a dict using mapper column introspection
  • is_schema() family of functions now correctly exclude SQLModel table models (they are ORM models, not transfer schemas)
  • schema_dump() returns SQLModel table instances as-is instead of calling model_dump()
  • New is_sqlmodel_table_model() detection function

Changes

File What
base.py Relaxed ModelProtocol (removed to_dict requirement), added model_to_dict()
service/typing.py Added is_sqlmodel_table_model(), updated is_schema() family to exclude table models, updated schema_dump()
service/_async.py / _sync.py Added SQLModel table model branch in to_model()
repository/_async.py / _sync.py Added SQLModel table model branch in to_model()
service/_typing.py Added SQLMODEL_INSTALLED flag
pyproject.toml Added sqlmodel to test dependencies
tests/unit/test_sqlmodel_compat.py 30 comprehensive tests covering all changes

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.85%. Comparing base (f5e02bb) to head (11db6ac).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
advanced_alchemy/alembic/utils.py 66.66% 1 Missing ⚠️
advanced_alchemy/repository/_async.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #686      +/-   ##
==========================================
+ Coverage   80.67%   80.85%   +0.18%     
==========================================
  Files          99       99              
  Lines        8177     8176       -1     
  Branches     1121     1119       -2     
==========================================
+ Hits         6597     6611      +14     
+ Misses       1249     1240       -9     
+ Partials      331      325       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cofin cofin changed the title feat: add SQLModel table=True compatibility feat: add SQLModel compatibility Mar 3, 2026
cofin added 6 commits March 3, 2026 12:01
Widen ModelProtocol to accept SQLModel table=True models by removing the
to_dict() requirement. Add model_to_dict() utility that delegates to
to_dict() for AA models and falls back to mapper-based column iteration
for SQLModel and other mapped models.

Add is_sqlmodel_table_model() detection to prevent the service layer from
misidentifying SQLModel table instances as plain Pydantic schemas in
schema_dump() and to_model().
The is_schema() family of functions incorrectly identified SQLModel
table=True models as schemas via the is_pydantic_model() check, causing
them to be decomposed into dicts instead of treated as ORM models.
Add sqlmodel as a test dependency and expand test coverage with
model_from_dict, get_primary_key_info, get_instrumented_attr, and
roundtrip tests for SQLModel table models.
Use cast() at call sites where SQLModel table=True models are passed to
functions typed with ModelProtocol. SQLModel's metaclass adds __mapper__
and __table__ dynamically, which satisfies ModelProtocol at runtime but
is invisible to static analysis. Also fix pyright errors in model_to_dict
by using getattr for duck-typed to_dict access and explicit type
annotations.
Add SQLModel compatibility, composite primary keys, read/write replica
routing, dogpile caching, and NULL filters to the feature list.
…coverage

Add tests for update_many, upsert, and upsert fallback-match-by-all-fields
using an in-memory SQLite database with SQLModel table models.
@cofin cofin merged commit 3153b49 into litestar-org:main Mar 8, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants