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
I used the GitHub search to find a similar question and didn't find it.
I searched the SQLModel documentation, with the integrated search.
I already searched in Google "How to X in SQLModel" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to SQLModel but to Pydantic.
I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
I commit to help with one of those options 👆
Example Code
WorkaroundsTried
✅ Introducedasharedregistry:
if"app.mapper_registry"notinsys.modules:
mapper_registry=registry()
sys.modules["foodpal.mapper_registry"] =mapper_registryelse:
mapper_registry=sys.modules["app.mapper_registry"]
✅ Clearedmetadataandclassregistryonreload:
if"src.core.models"insys.modules:
metadata.clear()
mapper_registry._class_registry.clear()
✅ Ensuredconsistentimports (singleBase, noduplicateimportpaths).
✅ Usedst.cache_resourceforconnection/providerpersistence.
importsysimportos# --- Ensure single registry across reloads ---if"app.mapper_registry"notinsys.modules:
mapper_registry=registry()
sys.modules["app.mapper_registry"] =mapper_registryelse:
mapper_registry=sys.modules["app.mapper_registry"]
metadata=mapper_registry.metadata# --- Guard: clear registry only if model classes already exist ---# This ensures we only clear if Streamlit *already* imported models before.if"src.core.models"insys.modules:
print("♻️ Streamlit reload detected — clearing stale SQLAlchemy state.")
try:
metadata.clear()
mapper_registry._class_registry.clear()
exceptExceptionase:
print("Warning while clearing registry:", e)
print("=======Registry id:========", id(mapper_registry))
Despitethis, thewarningspersistbecauseSQLModelstillattachesmodelstoitsowninternalSQLModel.metadataregistry.
Description
Environment
Framework: Streamlit (with st.cache_resource)
ORM: SQLModel (built on SQLAlchemy 2.x)
Language: Python 3.13
Context: Running a modularized app using SQLModel models and a custom registry, inside a Streamlit app that uses st.cache_resource to persist an SQLProvider instance.
Problem
Every time Streamlit performs a hot reload (e.g. when code changes or the script re-runs), the SQLModel models are re-imported, causing SQLAlchemy to re-register existing ORM classes on the same DeclarativeBase.
This leads to warnings such as:
SAWarning: This declarative base already contains a class with the same class name and module name as src.core.models.FoodItem, and will be replaced in the string-lookup table.
Even though we manage a shared registry across reloads via sys.modules and clear the metadata/registry on reload, SQLModel still re-adds models to its internal base and triggers these warnings.
Expected Behavior
SQLModel or SQLAlchemy should detect identical re-registrations and skip duplicates on reloads.
Streamlit hot reload should not cause multiple declarative model registrations for the same Base.
Actual Behavior
Each Streamlit reload causes SQLModel’s global declarative base to retain the previous model class definitions.
SQLModel re-registers new class objects with identical names → triggering repeated SAWarning.
This warning repeats for each ORM class on every reload.
In some configurations (without manual registry cleanup), it escalates to a sqlalchemy.exc.InvalidRequestError (“table already defined”).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Environment
Framework: Streamlit (with st.cache_resource)
ORM: SQLModel (built on SQLAlchemy 2.x)
Language: Python 3.13
Context: Running a modularized app using SQLModel models and a custom registry, inside a Streamlit app that uses st.cache_resource to persist an SQLProvider instance.
Problem
Every time Streamlit performs a hot reload (e.g. when code changes or the script re-runs), the SQLModel models are re-imported, causing SQLAlchemy to re-register existing ORM classes on the same DeclarativeBase.
This leads to warnings such as:
SAWarning: This declarative base already contains a class with the same class name and module name as src.core.models.FoodItem, and will be replaced in the string-lookup table.
Even though we manage a shared registry across reloads via sys.modules and clear the metadata/registry on reload, SQLModel still re-adds models to its internal base and triggers these warnings.
Expected Behavior
SQLModel or SQLAlchemy should detect identical re-registrations and skip duplicates on reloads.
Streamlit hot reload should not cause multiple declarative model registrations for the same Base.
Actual Behavior
Each Streamlit reload causes SQLModel’s global declarative base to retain the previous model class definitions.
SQLModel re-registers new class objects with identical names → triggering repeated SAWarning.
This warning repeats for each ORM class on every reload.
In some configurations (without manual registry cleanup), it escalates to a sqlalchemy.exc.InvalidRequestError (“table already defined”).
Operating System
macOS
Operating System Details
M1 Mac
OS: 26.0.1 (25A362)
SQLModel Version
0.0.27
Python Version
Python 3.13.7
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions