Rewrite the database docs around SQLAlchemy models

FA
4h ago
+953-370

Type of change

  • This change requires a documentation update

Description

The database overview, tables, queries and relationships pages and the rx.table database examples still taught the deprecated rx.Model and rx.session. Rather than adding deprecation callouts, this rewrites them around the supported pattern:

  • One shared base, class Base(MappedAsDataclass, DeclarativeBase). The dataclass mapping gives models a keyword constructor and lets Reflex serialize rows into state with no extra code.
  • engine = create_engine(rx.config.get_config().db_url) and Session = sessionmaker(engine, expire_on_commit=False), opened per event handler.
  • 2.0-style queries: session.scalars(select(User)), session.scalar(...), session.get(...), plus the async equivalents with async_sessionmaker.
  • rx.ModelRegistry.register(Base) so reflex db init / makemigrations / migrate keep managing the schema. Verified end to end against a scratch app.
  • Relationships: ForeignKey + relationship(..., repr=False), eager loading with selectinload, and a loaded-attributes @rx.serializer for the base so linked objects serialize without lazy loads on detached instances.
  • The overview gains a "Migrating existing apps" table mapping each rx.Model construct to its replacement. The SQLModel datetime upgrade note stays on the tables page under its existing anchor.

Also fixed along the way: the table page's CSV and JSON download examples used the Pydantic-only Customer.__fields__ and .dict(), which do not exist on these models. They now use dataclasses.

Testing

docs/app/tests/test_database_examples.py extracts the documented models, handlers and serializers from the pages and executes them against a SQLite file (CRUD, aggregates, raw SQL, relationships with eager loading, table load/filter/sort/paginate/download). The full docs suite passes.

Not touched here: the rx.Model usages on the dialog, alert dialog, enterprise AG Grid and code structure pages, which are outside the database section.

https://claude.ai/code/session_01LwJUCUDZhZY71q8Azr37BQ


Generated by Claude Code