FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

✨ Raise an exception when using a Pydantic field type with no matchin… · fastapi/sqlmodel@dc4dc42 · GitHub

Repository navigation

Commit dc4dc42

Browse files
andauthored
✨ 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>
1 parent db29f53 commit dc4dc42

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎sqlmodel/main.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ def get_sqlachemy_type(field: ModelField) -> Any:
415415
return AutoString
416416
if issubclass(field.type_, uuid.UUID):
417417
return GUID
418+
raise ValueError(f"The field {field.name} has no matching SQLAlchemy type")
418419

419420

420421
def get_column_from_field(field: ModelField) -> Column: # type: ignore

‎tests/test_missing_type.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Optional
2+
3+
import pytest
4+
from sqlmodel import Field, SQLModel
5+
6+
7+
def test_missing_sql_type():
8+
class CustomType:
9+
@classmethod
10+
def __get_validators__(cls):
11+
yield cls.validate
12+
13+
@classmethod
14+
def validate(cls, v):
15+
return v
16+
17+
with pytest.raises(ValueError):
18+
19+
class Item(SQLModel, table=True):
20+
id: Optional[int] = Field(default=None, primary_key=True)
21+
item: CustomType

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL