The Field regex attribute was renamed pattern in pydantic v2 #1298
-
| First Check
 Commit to Help
 Example Codefrom sqlmodel import Field, SQLModel
from pydantic import Field as PField, constr
class CarV1(SQLModel):
    license_plate: str = Field(regex=r"\w-\w{3}-\w{3}")  # Regex will be silently ignored
class CarV2(SQLModel):
    license_plate: str = Field(pattern=r"\w-\w{3}-\w{3}")  # Should be the new way but no pattern attribute
class CarV3(SQLModel):
    license_plate: str = Field(schema_extra=dict(pattern=r"\w-\w{3}-\w{3}"))  # Works but extra steps
class CarV4(SQLModel):
    license_plate: str = PField(pattern=r"\w-\w{3}-\w{3}")  # Works but we mix two different Field
class CarV5(SQLModel):
    license_plate: constr(pattern=r"\w-\w{3}-\w{3}")  # Works but we mix Field and constrDescription
 The regex is not validated and not documented in openapi.json. This is caused because Pydantic V2 does not use the regex attribute anymore but the pattern attribute instead: https://docs.pydantic.dev/latest/concepts/fields/#string-constraints In this case, it could be solved using Field or constr from Pydantic. Operating SystemLinux Operating System DetailsUbuntu 24.04 (WSL2) SQLModel Version0.0.22 Python Version3.12.3 Additional ContextNo response | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
| I ended doing even another way. Based on  from typing import Annotated
from pydantic import StringConstraints
from sqlmodel import Field, SQLModel
LicencePlate = Annotated[str, StringConstraints(pattern=r"\w-\w{3}-\w{3}")]
class Car(SQLModel):
    license_plate: LicensePlateIt seems cleaner like this and I can reuse the type. | 
Beta Was this translation helpful? Give feedback.
I ended doing even another way. Based on
EmailStrfrom pydantic and this answer: https://stackoverflow.com/a/67871116It seems cleaner like this and I can reuse the type.