mirror of
				https://github.com/fastapi/sqlmodel.git
				synced 2025-11-04 06:37:29 +08:00 
			
		
		
		
	✨ Raise an exception when using a Pydantic field type with no matching SQLAlchemy type (#18)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
		@ -415,6 +415,7 @@ def get_sqlachemy_type(field: ModelField) -> Any:
 | 
				
			|||||||
        return AutoString
 | 
					        return AutoString
 | 
				
			||||||
    if issubclass(field.type_, uuid.UUID):
 | 
					    if issubclass(field.type_, uuid.UUID):
 | 
				
			||||||
        return GUID
 | 
					        return GUID
 | 
				
			||||||
 | 
					    raise ValueError(f"The field {field.name} has no matching SQLAlchemy type")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_column_from_field(field: ModelField) -> Column:  # type: ignore
 | 
					def get_column_from_field(field: ModelField) -> Column:  # type: ignore
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										21
									
								
								tests/test_missing_type.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								tests/test_missing_type.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					from typing import Optional
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import pytest
 | 
				
			||||||
 | 
					from sqlmodel import Field, SQLModel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_missing_sql_type():
 | 
				
			||||||
 | 
					    class CustomType:
 | 
				
			||||||
 | 
					        @classmethod
 | 
				
			||||||
 | 
					        def __get_validators__(cls):
 | 
				
			||||||
 | 
					            yield cls.validate
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @classmethod
 | 
				
			||||||
 | 
					        def validate(cls, v):
 | 
				
			||||||
 | 
					            return v
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with pytest.raises(ValueError):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        class Item(SQLModel, table=True):
 | 
				
			||||||
 | 
					            id: Optional[int] = Field(default=None, primary_key=True)
 | 
				
			||||||
 | 
					            item: CustomType
 | 
				
			||||||
		Reference in New Issue
	
	Block a user