From 80f29355727a2815fffd84fb292fc0e821d24a2b Mon Sep 17 00:00:00 2001 From: Arun Sharma Date: Thu, 16 Jan 2025 20:53:55 -0800 Subject: [PATCH] registry: unregister the manager and class --- sqlmodel/main.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index cf65f8b572..d03fe7b2e4 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -591,6 +591,14 @@ def __init__( return cls.__do_init__(bases, dict_, kw) def sqlmodel_rebuild(cls) -> None: + reg = cls._sa_registry + # clear any exisiting mappers for the cls + manager = [m for m in reg._managers if m.class_ == cls] + if len(manager) > 0: + for m in manager: + reg._dispose_manager_and_mapper(m) + del reg._managers[m] + return cls.__do_init__(cls.__bases__, cls.__dict__, {}) # Override SQLAlchemy, allow both SQLAlchemy and plain Pydantic models