Repository navigation
Add support for versioning SQLModel models - #410
Conversation
Version and transaction classes are built on the model's declarative base. For SQLModel models that resolves to the SQLModel class, which isn't a declarative base, so mapper configuration failed with AttributeError: registry. When a model's base doesn't own its mapper registry, generate a base from that registry instead. The SQLModel tests run on the SQLAlchemy 2.x CI legs and tox envs via a new test-sqlmodel dependency group. Refs #271
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
|
The docs example declares models on SQLModel's default registry with user_cls=None, while the other SQLModel tests use a registry per test. Cover that setup too, including table placement in SQLModel.metadata, and dispose of the default registry afterwards so later declarations of the same names don't collide. Also add the missing :param: field to resolve_declarative_base.
|
@greptileai review |
Description
Adds support for versioning SQLModel table models. Closes #271 and supersedes #397. The reasoning for the simpler approach is in this comment.
Continuum builds version classes and the
Transactionclass on the model's declarative base. It finds that base by walking__bases__to the class that carriesmetadata. For a SQLModel model, that's theSQLModelclass itself, which isn't a SQLAlchemy declarative base: it has noregistry, and its metaclass only mapstable=Trueclasses. Mapper configuration therefore fails withAttributeError: registry. The workaround in #271 patchedsqlalchemy_utils.functions.get_declarative_base, which Continuum stopped calling when SQLAlchemy-Utils was dropped (#352), so there is currently no workaround.Fix
resolve_declarative_base(model)inmodel_builder.py, used for both the version class bases (get_base_class) and theTransactionbase (Builder.build_transaction_class).DeclarativeBaseand Flask-SQLAlchemy model, so their behavior is unchanged.registry.generate_base()for the model's registry.sa.inspect(model).registry, which is public API on SQLAlchemy 1.4 through 2.1. There's no cache, no SQLModel import and no runtime cost: it runs once per versioned root class during mapper configuration.Transactionper registry) is out of scope.Tests
tests/test_sqlmodel.pyruns under both versioning strategies, using SQLModel'sSession.declarative_base()inTestCase.base_classes=Nonethrough the normaloptions, so tests don't inherit a previous test's base.user_clsstring lookuplink_modelmany-to-many relationship versionsSessiondeprecatesquery()andexecute(), which the dynamicversionsrelationship and some version lookups call, so the module filters that warning.Dependencies and CI
test-sqlmodeldependency group, included indev. It's separate fromtestbecause SQLModel requires SQLAlchemy 2.x.sqlalchemy{20,21}envs.uv.lockonly adds sqlmodel, pydantic, pydantic-core, annotated-types and typing-inspection.Docs
Verification (local)
DB=sqlite) passes on Python 3.15 with SQLAlchemy 1.4, 2.0 and 2.1 (via tox), and on Python 3.11 with 2.0. SQLModel version: 0.0.48.zensical build --clean --strictpass.Possible follow-up (not in this PR): Continuum's own version lookups still call
session.query(). Moving them toselect()would remove most of the SQLModel deprecation warnings. The dynamicversionsrelationship would still trigger it.Checklist
This pull request is:
🤖 Generated with Claude Code
Summary by CodeRabbit