Skip to content

ValueError: <class 'pydantic.networks.EmailStr'> has no matching SQLAlchemy type #730

Closed Answered by rmasters
ahaly-compass asked this question in Questions
Discussion options

You must be logged in to vote

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 own AutoString wrapper type:

-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 EmailStr does not subclass a type that SQLModel knows how to convert to a SQLAlchemy type (i.e. it's not class EmailStr(str). For exa…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ahaly-compass
Comment options

@salahelfarissi
Comment options

@ChrisNi888
Comment options

Answer selected by ahaly-compass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
4 participants