Skip to content

Support sqlmodel_rebuild #1314

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__(bases, dict_, kw) # type: ignore

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.__bases__, cls.__dict__, {}) # type: ignore

# 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
Loading