You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Creating models fails with pydantic >= 2.12.0. Here is the error:
classProjectEntity(SQLModel, table=True):
../../../.venv/lib/python3.12/site-packages/sqlmodel/main.py:565: in__new__col=get_column_from_field(v)
^^^^^^^^^^^^^^^^^^^^^^^^
../../../.venv/lib/python3.12/site-packages/sqlmodel/main.py:714: inget_column_from_fieldsa_type=get_sqlalchemy_type(field)
^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../.venv/lib/python3.12/site-packages/sqlmodel/main.py:703: inget_sqlalchemy_typeraiseValueError(f"{type_} has no matching SQLAlchemy type")
EValueError: <class'dict'>hasnomatchingSQLAlchemytype
This same code has always worked for all pydantic versions prior to 2.12.0.
I tested with sqlmodel == 0.0.25 and sqlmodel == 0.0.26 and both fail when I bump pydantic to 2.12.0, even though the dependency resolver for sqlmodel allows for it to be upgraded.
Could you please share an MRE?
With the code from your initial comment I have
sqlalchemy.exc.ArgumentError: Mapper Mapper[ProjectEntity(project_entity)] could not assemble any primary key columns for mapped table 'project_entity'
@YuriiMotov sorry, I was trying to obfuscate some stuff because this is in a private repo. I created a full working example:
Works with sqlmodel (all versions) and pydantic < 2.12.0, e.g. 2.11.9
Fails for sqlmodel (all versions) and pydantic >= 2.12.0
fromtypingimportAnnotated, AnyimportsqlmodelfrompydanticimportAfterValidatorfromsqlalchemy.dialects.postgresqlimportJSONBfromsqlmodelimportField, SQLModeldefvalidate_json_length(value: dict[str, Any]) ->dict[str, Any]:
iflen(value) >10:
raiseAssertionError("JSON should have at most 10 keys")
returnvalueLengthConstrainedDict=Annotated[dict[str, Any], AfterValidator(validate_json_length)]
classTestProject(SQLModel, table=True):
__tablename__="test_project"project_id: str=Field(
description="The entity ID in database",
primary_key=True,
)
project_metadata: LengthConstrainedDict=sqlmodel.Field(
description="Metadata for the project",
default_factory=dict,
sa_type=JSONB,
)
The issue seems to be with the annotated type LengthConstrainedDict and the validator. If I change LengthConstrainedDict to just dict[str, Any], the error does not happen.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
First Check
Commit to Help
Example Code
Description
Creating models fails with
pydantic >= 2.12.0. Here is the error:This same code has always worked for all
pydanticversions prior to2.12.0.I tested with
sqlmodel == 0.0.25andsqlmodel == 0.0.26and both fail when I bumppydanticto2.12.0, even though the dependency resolver forsqlmodelallows for it to be upgraded.Operating System
macOS
Operating System Details
MacOS ARM 15.6.1
SQLModel Version
0.0.26
Python Version
Python 3.12.11
Additional Context
No response
All reactions