Skip to content

Commit ecc2f0e

Browse files
author
JSap0914
committed
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
1 parent 7512b96 commit ecc2f0e

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

sqlmodel/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,11 @@ def get_config(name: str) -> Any:
613613
# TODO: remove this in the future
614614
new_cls.model_config["read_with_orm_mode"] = True # ty: ignore[invalid-key]
615615

616-
config_registry = get_config("registry")
616+
config_registry = kwargs.get("registry", Undefined)
617617
if config_registry is not Undefined:
618618
config_registry = cast(registry, config_registry)
619619
# If it was passed by kwargs, ensure it's also set in config
620-
new_cls.model_config["registry"] = config_table
620+
new_cls.model_config["registry"] = config_registry
621621
setattr(new_cls, "_sa_registry", config_registry) # noqa: B010
622622
setattr(new_cls, "metadata", config_registry.metadata) # noqa: B010
623623
setattr(new_cls, "__abstract__", True) # noqa: B010

tests/test_registry_kwarg.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Tests for SQLModelMetaclass.__new__ registry kwarg handling."""
2+
import pytest
3+
from sqlalchemy.orm import registry
4+
from sqlmodel import SQLModel
5+
6+
7+
def test_custom_registry_base_stores_registry_in_model_config() -> None:
8+
"""model_config['registry'] must hold the registry object passed as kwarg."""
9+
custom_registry = registry()
10+
11+
class MyBase(SQLModel, registry=custom_registry):
12+
pass
13+
14+
stored = MyBase.model_config.get("registry")
15+
assert stored is custom_registry, (
16+
f"model_config['registry'] should be the custom registry, got {stored!r}"
17+
)
18+
19+
20+
def test_custom_registry_base_sets_sa_registry() -> None:
21+
"""_sa_registry must reference the registry object passed as kwarg."""
22+
custom_registry = registry()
23+
24+
class MyBase2(SQLModel, registry=custom_registry):
25+
pass
26+
27+
sa_registry = getattr(MyBase2, "_sa_registry", None)
28+
assert sa_registry is custom_registry, (
29+
f"_sa_registry should be the custom registry, got {sa_registry!r}"
30+
)

0 commit comments

Comments
 (0)