Skip to content

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

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

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.

This branch has not been deployed

No deployments
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.

1 participant