@@ -589,12 +589,12 @@ def __new__(
589589 }
590590
591591 def get_config (name : str ) -> Any :
592- config_class_value = new_cls .model_config .get (name , Undefined )
593- if config_class_value is not Undefined :
594- return config_class_value
595592 kwarg_value = kwargs .get (name , Undefined )
596593 if kwarg_value is not Undefined :
597594 return kwarg_value
595+ config_class_value = new_cls .model_config .get (name , Undefined )
596+ if config_class_value is not Undefined :
597+ return config_class_value
598598 return Undefined
599599
600600 config_table = get_config ("table" )
@@ -618,10 +618,15 @@ def get_config(name: str) -> Any:
618618 if config_registry is not Undefined :
619619 config_registry = cast (registry , config_registry )
620620 # If it was passed by kwargs, ensure it's also set in config
621- new_cls .model_config ["registry" ] = config_table
622- setattr (new_cls , "_sa_registry" , config_registry ) # noqa: B010
623- setattr (new_cls , "metadata" , config_registry .metadata ) # noqa: B010
624- setattr (new_cls , "__abstract__" , True ) # noqa: B010
621+ new_cls .model_config ["registry" ] = config_registry
622+ # Only set up the registry attributes when explicitly passed
623+ # as a kwarg on this class, not when inherited from a parent.
624+ # Setting __abstract__ on subclasses that merely inherit the
625+ # registry would prevent SQLAlchemy from instrumenting them.
626+ if "registry" in kwargs :
627+ setattr (new_cls , "_sa_registry" , config_registry ) # noqa: B010
628+ setattr (new_cls , "metadata" , config_registry .metadata ) # noqa: B010
629+ setattr (new_cls , "__abstract__" , True ) # noqa: B010
625630 return new_cls
626631
627632 # Override SQLAlchemy, allow both SQLAlchemy and plain Pydantic models
0 commit comments