Skip to content

Column with list[Enum] dtype silently removes value if not a valid enum string #500

Closed
@Vlados09

Description

@Vlados09

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

import enum
from sqlmodel import SQLModel, Field


class SomeEnum(enum.Enum):
    VALUE_1 = "value_1"
    VALUE_2 = "value_2"


class ModelWithEnumList(SQLModel, table=True):
    key: int = Field(primary_key=True)
    value: list[SomeEnum]

if __name__ == "__main__":
    # This Works Well:
    instance_1 = ModelWithEnumList(key=1, value=["value_1", "value_2"])
    assert len(instance_1.value) == 2
    assert instance_1.value == [SomeEnum.VALUE_1, SomeEnum.VALUE_2]

    # This just silently drops the value...
    instance_2 = ModelWithEnumList(key=2, value=["value_1", "value_2", "value_3"])
    assert len(instance_2.value) == 3, "Something weird happens here, the field just gets dropped silently"

Description

SQLModel with table=True silently drops the field for a list[Enum] with invalid Enum. Correct error is raised without table=True and using pydantic BaseModel so assume this is purely sqlmodel bug.

Operating System

Linux

Operating System Details

Docker, using python3.10-slim image.

SQLModel Version

0.0.8

Python Version

3.10

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions