Skip to content

[Question] Why are pydantic validation results ignored for models with table=True? Where are the benefits? #406

Closed
@Cielquan

Description

@Cielquan

First Check

EDIT: See #406 (comment)

  • 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

import enum
from pydantic import BaseModel, ValidationError
from sqlmodel import Field, SQLModel


class EnumValues(str, enum.Enum):

    FOO = "foo"
    BAR = "bar"


class TableModel(BaseModel):
    value: EnumValues


print(TableModel(value="bar"))  # value=<EnumValues.BAR: 'bar'>
try:
    print(TableModel(value="baz"))
except ValidationError as e:
    print(e)
    # 1 validation error for TableModel
    # value
    #   value is not a valid enumeration member; permitted: 'foo', 'bar' (type=type_error.enum; enum_values=[<EnumValues.FOO: 'foo'>, <EnumValues.BAR: 'bar'>])


class TableBase(SQLModel):
    value: EnumValues


print(TableBase(value="bar"))  # value=<EnumValues.BAR: 'bar'>
try:
    print(TableBase(value="baz"))
except ValidationError as e:
    print(e)
    # 1 validation error for TableBase
    # value
    #   value is not a valid enumeration member; permitted: 'foo', 'bar' (type=type_error.enum; enum_values=[<EnumValues.FOO: 'foo'>, <EnumValues.BAR: 'bar'>])


class Table(TableBase, table=True):
    id: int | None = Field(default=None, nullable=False, primary_key=True)  # noqa: VNE003


print(Table(value="bar"))  # value=<EnumValues.BAR: 'bar'> id=None
try:
    print(Table(value="baz"))  # id=None
except ValidationError as e:
    print(e)  # NEVER REACHED

Description

  • make a instance of the TableModel which is a pure pydantic model with a valid string in the enum -> success (as it should be)
  • make a instance of the TableModel which is a pure pydantic model with an invalid string in the enum -> error (as it should be)
  • make a instance of the TableBase which is not table=True with a valid string in the enum -> success (as it should be)
  • make a instance of the TableBase which is not table=True with an invalid string in the enum -> error (as it should be)
  • make a instance of the Table which is table=True with a valid string in the enum -> success (as it should be)
  • make a instance of the Table which is table=True with an invalid string in the enum -> success (should be error, but creates an instance which is totally missing the non-optional and non-default value field)

Operating System

Linux

Operating System Details

Ubuntu

SQLModel Version

0.0.6

Python Version

3.10.4

Additional Context

No response

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