]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/commitdiff
✨ Raise an exception when using a Pydantic field type with no matching SQLAlchemy...
authorJakob Jul Elben <elbenjakobjul@gmail.com>
Sat, 27 Aug 2022 20:13:32 +0000 (22:13 +0200)
committerGitHub <noreply@github.com>
Sat, 27 Aug 2022 20:13:32 +0000 (20:13 +0000)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
sqlmodel/main.py
tests/test_missing_type.py [new file with mode: 0644]

index 63c6dcbe5f6b002b3712a42729b5e6643e0b7489..9efdafeca36eea829cfcfb1d6ade19a740b5016b 100644 (file)
@@ -415,6 +415,7 @@ def get_sqlachemy_type(field: ModelField) -> Any:
         return AutoString
     if issubclass(field.type_, uuid.UUID):
         return GUID
+    raise ValueError(f"The field {field.name} has no matching SQLAlchemy type")
 
 
 def get_column_from_field(field: ModelField) -> Column:  # type: ignore
diff --git a/tests/test_missing_type.py b/tests/test_missing_type.py
new file mode 100644 (file)
index 0000000..2185fa4
--- /dev/null
@@ -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