ValueError: <class 'pydantic.networks.EmailStr'> has no matching SQLAlchemy type #730
-
First Check
Commit to Help
Example Codefrom pydantic import EmailStr
from sqlmodel import Field, SQLModel
class UserBase(SQLModel):
name: str = Field(index=True)
first_name: str
last_name: str
email: EmailStr = Field(unique=True, index=True)
active: bool = Field(default=True)
# # Database model, database table inferred from class name
class User(UserBase, table=True):
__tablename__ = "users"
id: int | None = Field(default=None, primary_key=True)
hashed_password: str
# tickets: list["Ticket"] = Relationship(back_populates="ticket") Description
ValueError: <class 'pydantic.networks.EmailStr'> has no matching SQLAlchemy type relevant dependencies:
Operating SystemLinux Operating System DetailsUbuntu 22.04 WSL SQLModel Version0.0.14 Python Version3.12 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
I've run into this a few times. The fix is to pass the SQLAlchemy data type directly - this is -from sqlmodel import Field, SQLModel
+from sqlmodel import Field, SQLModel, AutoString
class UserBase(SQLModel):
name: str = Field(index=True)
first_name: str
last_name: str
- email: EmailStr = Field(unique=True, index=True)
+ email: EmailStr = Field(unique=True, index=True, sa_type=AutoString)
active: bool = Field(default=True) The root cause of this is
|
Beta Was this translation helpful? Give feedback.
I've run into this a few times. The fix is to pass the SQLAlchemy data type directly - this is
sqlalchemy.String()
, but in this case I'm using sqlmodel's ownAutoString
wrapper type:The root cause of this is
EmailStr
does not subclass a type that SQLModel knows how to convert to a SQLAlchemy type (i.e. it's notclass EmailStr(str)
. For exa…