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

When "table=True", the type is not checked. #324

Closed
8 tasks done
phi-friday opened this issue May 2, 2022 · 2 comments
Closed
8 tasks done

When "table=True", the type is not checked. #324

phi-friday opened this issue May 2, 2022 · 2 comments
Labels
question Further information is requested

Comments

@phi-friday
Copy link
Contributor

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 pydantic import ValidationError
from sqlmodel import Field, SQLModel


class test_base(SQLModel):
    name: str | None = None
    value: float | None = None


class test_strict(test_base):
    name: str
    value: float


class test_table(test_base, table=True):
    id: int | None = Field(None, primary_key=True)
    name: str
    value: float


class test_table_from_strict(test_strict, table=True):
    id: int | None = Field(None, primary_key=True)


class test_table_not_override(SQLModel, table=True):
    id: int | None = Field(None, primary_key=True)
    name: str
    value: float


test_values = [
    {},
    {"name": "test"},
    {"name": "test", "value": 123},
    {"value": 545},
    {"name": "test", "value": "test"},
]

for value in test_values:
    print(f"{value=}")
    for _class in (
        test_strict,
        test_table,
        test_table_from_strict,
        test_table_not_override,
    ):
        print(_class.__name__, end=": ")
        try:
            val = _class(**value)
        except ValidationError as exc:
            print(exc)
        else:
            print(val)
    print()

"""
value={}
test_strict: 2 validation errors for test_strict
name
  field required (type=value_error.missing)
value
  field required (type=value_error.missing)
test_table: id=None
test_table_from_strict: id=None
test_table_not_override: id=None

value={'name': 'test'}
test_strict: 1 validation error for test_strict
value
  field required (type=value_error.missing)
test_table: name='test' id=None
test_table_from_strict: name='test' id=None
test_table_not_override: id=None name='test'

value={'name': 'test', 'value': 123}
test_strict: name='test' value=123.0
test_table: name='test' value=123.0 id=None
test_table_from_strict: name='test' value=123.0 id=None
test_table_not_override: id=None name='test' value=123.0

value={'value': 545}
test_strict: 1 validation error for test_strict
name
  field required (type=value_error.missing)
test_table: value=545.0 id=None
test_table_from_strict: value=545.0 id=None
test_table_not_override: id=None value=545.0

value={'name': 'test', 'value': 'test'}
test_strict: 1 validation error for test_strict
value
  value is not a valid float (type=type_error.float)
test_table: name='test' id=None
test_table_from_strict: name='test' id=None
test_table_not_override: id=None name='test'
"""

Description

Contrary to what I thought,
An error should occur for the same reason as "test_strict", but it does not actually occur.

Operating System

Linux

Operating System Details

OS: Ubuntu 18.04.6 LTS x86_64
Host: Virtual Machine 7.0
Kernel: 5.4.0-1070-azure
Uptime: 59 days, 16 hours, 31 mins
Packages: 1492
Shell: zsh 5.4.2
Resolution: 1920x1080, 2560x1080
WM: GNOME Shell
WM Theme: Adwaita
Theme: Adwaita [GTK3]
Icons: Adwaita [GTK3]
Terminal: vscode
CPU: Intel Xeon E5-2673 v4 (2) @ 2.294GHz
GPU: Microsoft Corporation Hyper-V virtual VGA
Memory: 4553MiB / 16010MiB

SQLModel Version

0.0.6

Python Version

Python 3.10.3

Additional Context

No response

@phi-friday phi-friday added the question Further information is requested label May 2, 2022
@phi-friday
Copy link
Contributor Author

no from_orm, no parse_obj,
use validate method.

@eudu
Copy link

eudu commented Jun 30, 2022

Duplicate of #134 and #52

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

2 participants