Raise a more clear error when a type is not valid (#425)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
David Danier 2023-10-23 08:42:30 +02:00 committed by GitHub
parent a8a792e3c0
commit 840fd08ab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 39 deletions

View File

@ -374,6 +374,7 @@ class SQLModelMetaclass(ModelMetaclass, DeclarativeMeta):
def get_sqlalchemy_type(field: ModelField) -> Any:
if isinstance(field.type_, type) and field.shape == SHAPE_SINGLETON:
if issubclass(field.type_, str):
if field.field_info.max_length:
return AutoString(length=field.field_info.max_length)

View File

@ -0,0 +1,28 @@
from typing import Any, Dict, List, Optional, Union
import pytest
from sqlmodel import Field, SQLModel
def test_type_list_breaks() -> None:
with pytest.raises(ValueError):
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
tags: List[str]
def test_type_dict_breaks() -> None:
with pytest.raises(ValueError):
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
tags: Dict[str, Any]
def test_type_union_breaks() -> None:
with pytest.raises(ValueError):
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
tags: Union[int, str]