Skip to content

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

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

Closed
8 tasks done
Vlados09 opened this issue Nov 14, 2022 · 1 comment
Closed
8 tasks done
Labels
investigate question Further information is requested

Comments

@Vlados09
Copy link

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

@Vlados09 Vlados09 added the question Further information is requested label Nov 14, 2022
@meirdev
Copy link

meirdev commented Nov 15, 2022

This behavior seems to be intentional:

https://github.com/tiangolo/sqlmodel/blob/5fa9062ed94b2be090ef9d15ea6666d347d1a2a6/sqlmodel/main.py#L501-L506

You can find more discussion of this in #52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants