@@ -1221,44 +1221,45 @@ def _convert_single_pydantic_to_table_model(item: Any, target_type: Any) -> Any:
12211221 if item is None :
12221222 return item
12231223
1224- # If target_type is a string (forward reference), try to resolve it
1224+ resolved_target_type = target_type
12251225 if isinstance (target_type , str ):
12261226 try :
1227- resolved_type = default_registry ._class_registry .get (target_type )
1228- if resolved_type is not None :
1229- target_type = resolved_type
1227+ # Attempt to resolve forward reference from the default registry
1228+ # This was part of the original logic and should be kept
1229+ resolved_type_from_registry = default_registry ._class_registry .get (target_type )
1230+ if resolved_type_from_registry is not None :
1231+ resolved_target_type = resolved_type_from_registry
12301232 except Exception :
1231- pass
1232-
1233- # If target_type is still a string after resolution attempt,
1234- # we can't perform type checks or conversions
1235- if isinstance (target_type , str ):
1236- # If item is a BaseModel but not a table model, try conversion
1237- if (
1238- isinstance (item , BaseModel )
1239- and hasattr (item , "__class__" )
1240- and not is_table_model_class (item .__class__ )
1241- ):
1242- # Can't convert without knowing the actual target type
1243- return item
1244- else :
1245- return item
1233+ # If resolution fails, and it's still a string, we might not be able to convert
1234+ # However, the original issue implies 'relationship_to' in the caller
1235+ # `_convert_pydantic_to_table_model` should provide a resolved type.
1236+ # For safety, if it's still a string here, and item is a simple Pydantic model,
1237+ # it's best to return item to avoid errors if no concrete type is found.
1238+ if isinstance (resolved_target_type , str ) and isinstance (item , BaseModel ) and hasattr (item , "__class__" ) and not is_table_model_class (item .__class__ ):
1239+ return item # Fallback if no concrete type can be determined
1240+ pass # Continue if resolved_target_type is now a class or item is not a simple Pydantic model
1241+
1242+ # If resolved_target_type is still a string and not a class, we cannot proceed with conversion.
1243+ # This can happen if the forward reference cannot be resolved.
1244+ if isinstance (resolved_target_type , str ):
1245+ return item
12461246
12471247 # If item is already the correct type, return as-is
1248- if isinstance (item , target_type ):
1248+ if isinstance (item , resolved_target_type ):
12491249 return item
12501250
1251- # Check if target_type is a SQLModel table class
1251+ # Check if resolved_target_type is a SQLModel table class
1252+ # This check should be on resolved_target_type, not target_type
12521253 if not (
1253- hasattr (target_type , "__mro__" )
1254+ hasattr (resolved_target_type , "__mro__" )
12541255 and any (
1255- hasattr (cls , "__sqlmodel_relationships__" ) for cls in target_type .__mro__
1256+ hasattr (cls , "__sqlmodel_relationships__" ) for cls in resolved_target_type .__mro__
12561257 )
12571258 ):
12581259 return item
12591260
1260- # Check if target is a table model
1261- if not is_table_model_class (target_type ):
1261+ # Check if target is a table model using resolved_target_type
1262+ if not is_table_model_class (resolved_target_type ):
12621263 return item
12631264
12641265 # Check if item is a BaseModel (Pydantic model) but not a table model
@@ -1277,8 +1278,8 @@ def _convert_single_pydantic_to_table_model(item: Any, target_type: Any) -> Any:
12771278 # Pydantic v1
12781279 data = item .dict ()
12791280
1280- # Create new table model instance
1281- return target_type (** data )
1281+ # Create new table model instance using resolved_target_type
1282+ return resolved_target_type (** data )
12821283 except Exception :
12831284 # If conversion fails, return original item
12841285 return item
0 commit comments