I'm wondering if this is how fields like max_length should work #746
-
First Check
Commit to Help
Example Codeclass Role(str, Enum):
ADMIN = "admin"
EDITOR = "editor"
class User(SQLModel, table=True):
__tablename__ = "user"
id: int | None = Field(default=None, primary_key=True)
username: str = Field(max_length=25)
email: EmailStr = Field(unique=True)
password: str = Field(nullable=False)
role: Role = Field(default=Role.EDITOR)
created_at: datetime = Field(default_factory=datetime.utcnow)
is_active: bool = Field(default=False) DescriptionI'm wondering if this is how fields like max_length should work. Hi, if you look at the SQLModel class below, you can see that the maximum field length for So I expected the SQL equivalent of this to be something like ![]() Should I use sa_column for this? Or is there another way and I'm missing something? Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.14 Python Version3.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
You can use sa_type argument Try this:
|
Beta Was this translation helpful? Give feedback.
-
I've just stumbled on this issue too. This was working as expected when targeting Pydantic V1, so think it is a bug. The I dug a little deeper and the issue appears to be in a function in
The configured max length is in field.metadata, but as an annotated type, not |
Beta Was this translation helpful? Give feedback.
-
I opened a small PR (#857) that seems to fix the issue by checking for the case mentioned above. |
Beta Was this translation helpful? Give feedback.
-
I am using a combination of max_length and sa_type
it seems to kinda work, but mypy is really mad about it:
in SQLModel the type for
|
Beta Was this translation helpful? Give feedback.
-
In current version (0.0.24) it already works this way: from enum import Enum
from sqlmodel import SQLModel, Field, create_engine
class Role(str, Enum):
ADMIN = "admin"
EDITOR = "editor"
class User(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
username: str = Field(max_length=25)
role: Role
engine = create_engine("sqlite:///", echo=True)
SQLModel.metadata.create_all(engine) Output:
|
Beta Was this translation helpful? Give feedback.
In current version (0.0.24) it already works this way:
Output: