Skip to content

fix: validate fields on instantiation of table=True SQLModel classes - #2144

Closed
viniciuswnbr wants to merge 1 commit into
fastapi:mainfrom
viniciuswnbr:fix/table-model-validation-1041
Closed

viniciuswnbr wants to merge 1 commit into
fastapi:mainfrom
viniciuswnbr:fix/table-model-validation-1041

Conversation

@viniciuswnbr

Copy link
Copy Markdown

Fixes #1041

Summary

Previously, instantiating a SQLModel with table=True and keyword arguments bypassed Pydantic validation by calling sqlmodel_table_construct() directly. As a result, invalid field types or constraint violations (e.g., gt, min_length) did not raise ValidationError on model creation.

Changes

  • Updated sqlmodel_init() in sqlmodel/_compat.py to validate passed field values via __pydantic_validator__.validate_python() when table=True and initialization arguments are provided, while preserving SQLAlchemy instrumentation (_sa_instance_state), __pydantic_fields_set__, relationship assignments, and argument-less instantiation (Model()).
  • Added unit tests in tests/test_table_validation.py covering type enforcement, Field constraints, type coercion with SQLite persistence, and relationship initialization.

@YuriiMotov

Copy link
Copy Markdown
Member

Thanks for your interest and efforts, but please follow the rules of the repository.

We ask people to only open PRs when it's explicitly requested by maintainers.
See https://tiangolo.com/open-source/contributing/ and PR description template:

Please start with a GitHub Discussion.

Once a team member asks you to open a PR, create it and link the discussion here.

I will close this for now to keep things clean.
Thanks for understanding!

@YuriiMotov YuriiMotov closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants