From ecc2f0e30dd1c3161b5d66432d670d32000b642d Mon Sep 17 00:00:00 2001 From: JSap0914 Date: Wed, 17 Jun 2026 15:45:47 +0900 Subject: [PATCH 1/2] fix: use kwargs registry directly and store it in model_config In SQLModelMetaclass.__new__, the registry configuration block had two bugs: 1. config_registry was sourced via get_config("registry"), which reads the inherited model_config first. Once any parent class sets model_config["registry"] (as SQLModel itself does), subclasses that pass a custom registry=... kwarg would silently get the parent's registry instead of their own. 2. model_config["registry"] was assigned config_table (a copy-paste error) instead of config_registry, so the model_config entry always ended up as PydanticUndefined rather than the actual registry object. Fix both by reading directly from kwargs (honoring the kwarg for the class being defined without inheriting an ancestor's value) and writing the correct variable. Reproducer: custom_registry = registry() class MyBase(SQLModel, registry=custom_registry): pass # Before: MyBase.model_config["registry"] == PydanticUndefined # After: MyBase.model_config["registry"] is custom_registry --- sqlmodel/main.py | 4 ++-- tests/test_registry_kwarg.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/test_registry_kwarg.py diff --git a/sqlmodel/main.py b/sqlmodel/main.py index c551afea36..fed132dbcc 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -613,11 +613,11 @@ def get_config(name: str) -> Any: # TODO: remove this in the future new_cls.model_config["read_with_orm_mode"] = True # ty: ignore[invalid-key] - config_registry = get_config("registry") + config_registry = kwargs.get("registry", Undefined) if config_registry is not Undefined: config_registry = cast(registry, config_registry) # If it was passed by kwargs, ensure it's also set in config - new_cls.model_config["registry"] = config_table + new_cls.model_config["registry"] = config_registry setattr(new_cls, "_sa_registry", config_registry) # noqa: B010 setattr(new_cls, "metadata", config_registry.metadata) # noqa: B010 setattr(new_cls, "__abstract__", True) # noqa: B010 diff --git a/tests/test_registry_kwarg.py b/tests/test_registry_kwarg.py new file mode 100644 index 0000000000..0326a63e45 --- /dev/null +++ b/tests/test_registry_kwarg.py @@ -0,0 +1,30 @@ +"""Tests for SQLModelMetaclass.__new__ registry kwarg handling.""" +import pytest +from sqlalchemy.orm import registry +from sqlmodel import SQLModel + + +def test_custom_registry_base_stores_registry_in_model_config() -> None: + """model_config['registry'] must hold the registry object passed as kwarg.""" + custom_registry = registry() + + class MyBase(SQLModel, registry=custom_registry): + pass + + stored = MyBase.model_config.get("registry") + assert stored is custom_registry, ( + f"model_config['registry'] should be the custom registry, got {stored!r}" + ) + + +def test_custom_registry_base_sets_sa_registry() -> None: + """_sa_registry must reference the registry object passed as kwarg.""" + custom_registry = registry() + + class MyBase2(SQLModel, registry=custom_registry): + pass + + sa_registry = getattr(MyBase2, "_sa_registry", None) + assert sa_registry is custom_registry, ( + f"_sa_registry should be the custom registry, got {sa_registry!r}" + ) From 47d5be9b5f3c53dd7574c0c137ccef346d4078c8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 17 Jun 2026 06:46:49 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_registry_kwarg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_registry_kwarg.py b/tests/test_registry_kwarg.py index 0326a63e45..c121b009ca 100644 --- a/tests/test_registry_kwarg.py +++ b/tests/test_registry_kwarg.py @@ -1,5 +1,5 @@ """Tests for SQLModelMetaclass.__new__ registry kwarg handling.""" -import pytest + from sqlalchemy.orm import registry from sqlmodel import SQLModel