Based on my research, I understand the issue. The `TypeSerializer` class in Django's migration serializer uses `__name__` instead of `__qualname__`, which causes incorrect paths for inner/nested classes. The fix is to replace `__name__` with `__qualname__` in two places within the `TypeSerializer.serialize()` method. --- a/django/db/migrations/serializer.py +++ b/django/db/migrations/serializer.py @@ -268,9 +268,9 @@ class TypeSerializer(BaseSerializer): if self.value is case: return string, set(imports) if hasattr(self.value, "__module__"): module = self.value.__module__ if module == builtins.__name__: - return self.value.__name__, set() + return self.value.__qualname__, set() else: - return "%s.%s" % (module, self.value.__name__), {"import %s" % module} + return "%s.%s" % (module, self.value.__qualname__), {"import %s" % module} raise ValueError( "Cannot serialize type: %s" % self.value.__name__