Skip to content

max_length does not work in Fields #126

Closed

Description

@yudjinn

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from sqlmodel import SQLModel, Field


class Locations(SQLModel, table=True):
    LocationName: str = Field(max_length=255)

Description

When running the above code, I get a
ValueError: On field "LocationName" the following field constraints are set but not enforced: max_length

I also tried pydantic's documentation by changing the 'str' typing to constr(max_length=255) to no avail.

Operating System

Windows

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

3.9.4

Additional Context

No response

Activity

  1. yasamoka commented on Oct 11, 2021

    @yasamoka

    Try this:

    from sqlmodel import SQLModel, Field, Column, String
    
    
    class Locations(SQLModel, table=True):
        LocationName: str = Field(sa_column=Column(String(255)))
    
  2. yudjinn commented on Oct 11, 2021

    @yudjinn
    Author

    This started working for some reason, genuinely not sure what resolved; maybe new version of fastapi?

  3. yasamoka commented on Oct 11, 2021

    @yasamoka

    How is it even related to FastAPI? SQLModel is still at version 0.0.4.

  4. yudjinn commented on Oct 12, 2021

    @yudjinn
    Author

    Fair point. I'm just genuinely not sure why it started working. waited a couple days, didnt even close the project, and it started working.

  5. yudjinn commented on Oct 12, 2021

    @yudjinn
    Author

    I ended up just leaving the max_length args in there and it seemed to start working. could not tell you why the errors earlier were consistently getting thrown.

  6. PaleNeutron commented on Jan 10, 2024

    @PaleNeutron

    2 years passed, is There any official solution?

  7. patrickwasp commented on Apr 3, 2024

    @patrickwasp

    @PaleNeutron this is what worked for me when migrating the model to mysql8. Just adding the max_length field did not change the size of the varchar.

    class Hero(SQLModel, table=True):
        id: int | None = Field(default=None, primary_key=True)
        name: str = Field.Field(
            max_length=1024, sa_column=sqlalchemy.Column(sqlalchemy.String(1024))
        )
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions