From dc4dc42ec5732e0467ae6f8df449998462ef242e Mon Sep 17 00:00:00 2001 From: Jakob Jul Elben Date: Sat, 27 Aug 2022 22:13:32 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Raise=20an=20exception=20when=20usi?= =?UTF-8?q?ng=20a=20Pydantic=20field=20type=20with=20no=20matching=20SQLAl?= =?UTF-8?q?chemy=20type=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sebastián Ramírez --- sqlmodel/main.py | 1 + tests/test_missing_type.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/test_missing_type.py diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 63c6dcb..9efdafe 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -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 index 0000000..2185fa4 --- /dev/null +++ b/tests/test_missing_type.py @@ -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