Fixes an issue where configuring a custom SQLAlchemy registry stored config_table into model_config["registry"] instead of config_registry, and ensures that concrete table models subclassing a custom base model are properly mapped.
Problem
In SQLModelMetaclass.__new__:
config_registry=get_config("registry")
ifconfig_registryisnotUndefined:
config_registry=cast(registry, config_registry)
# If it was passed by kwargs, ensure it's also set in confignew_cls.model_config["registry"] =config_table# BUG: should be config_registrysetattr(new_cls, "_sa_registry", config_registry)
setattr(new_cls, "metadata", config_registry.metadata)
setattr(new_cls, "__abstract__", True)
new_cls.model_config["registry"] was assigned config_table (which is Undefined or a bool), rather than the actual config_registry object.
Furthermore, because model_config is inherited by subclasses in Pydantic v2, reading get_config("registry") from inherited model_config would trigger the registry block on every subclass, unconditionally setting __abstract__ = True on concrete table classes inheriting from the custom base.
Solution
Gate the custom registry definition to classes that explicitly declare registry via class kwargs (kwargs.get("registry")) or explicit class_dict["model_config"].
Add an end-to-end regression test in tests/test_main.py testing custom base registry definition, table subclassing, mapper initialization, and DB session operations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes an issue where configuring a custom SQLAlchemy registry stored config_table into model_config["registry"] instead of config_registry, and ensures that concrete table models subclassing a custom base model are properly mapped.
Problem
In SQLModelMetaclass.__new__:
new_cls.model_config["registry"] was assigned config_table (which is Undefined or a bool), rather than the actual config_registry object.
Furthermore, because model_config is inherited by subclasses in Pydantic v2, reading get_config("registry") from inherited model_config would trigger the registry block on every subclass, unconditionally setting __abstract__ = True on concrete table classes inheriting from the custom base.
Solution