Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max_length does not work in Fields #126

Closed
8 tasks done
yudjinn opened this issue Oct 8, 2021 · 7 comments
Closed
8 tasks done

max_length does not work in Fields #126

yudjinn opened this issue Oct 8, 2021 · 7 comments
Labels
question Further information is requested

Comments

@yudjinn
Copy link

yudjinn commented Oct 8, 2021

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

@yudjinn yudjinn added the question Further information is requested label Oct 8, 2021
@yasamoka
Copy link

yasamoka commented Oct 11, 2021

Try this:

from sqlmodel import SQLModel, Field, Column, String


class Locations(SQLModel, table=True):
    LocationName: str = Field(sa_column=Column(String(255)))

@yudjinn
Copy link
Author

yudjinn commented Oct 11, 2021

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

@yudjinn yudjinn closed this as completed Oct 11, 2021
@yasamoka
Copy link

yasamoka commented Oct 11, 2021

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

@yudjinn
Copy link
Author

yudjinn commented Oct 12, 2021

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.

@yudjinn
Copy link
Author

yudjinn commented Oct 12, 2021

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.

@PaleNeutron
Copy link

2 years passed, is There any official solution?

@patrickwasp
Copy link

@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
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants