Skip to content

✨ Support sqlmodel_rebuild #1270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
17 changes: 16 additions & 1 deletion sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,24 @@ def get_config(name: str) -> Any:
setattr(new_cls, "__abstract__", True) # noqa: B010
return new_cls

# Override SQLAlchemy, allow both SQLAlchemy and plain Pydantic models
def __init__(
cls, classname: str, bases: Tuple[type, ...], dict_: Dict[str, Any], **kw: Any
) -> None:
return cls.__do_init__(classname, bases, dict_, **kw)

def sqlmodel_rebuild(cls) -> None:
reg = cls._sa_registry
# clear any exisiting mappers for the cls
managers = [m for m in reg._managers if m.class_ == cls]
for m in managers:
reg._dispose_manager_and_mapper(m)
del reg._managers[m]

return cls.__do_init__(cls.__name__, cls.__bases__, cls.__dict__) # type: ignore # Dict vs Mapping

# Override SQLAlchemy, allow both SQLAlchemy and plain Pydantic models
def __do_init__(
cls, classname: str, bases: Tuple[type, ...], dict_: Dict[str, Any], **kw: Any
) -> None:
# Only one of the base classes (or the current one) should be a table model
# this allows FastAPI cloning a SQLModel for the response_model without
Expand Down