Skip to content

SQLModel fails to create DB constraints when using Annotated types after Pydantic 2.12.0 #1623

@YuriiMotov

Description

@YuriiMotov

Discussed in #1597

Originally posted by MarishLakshmanan October 8, 2025

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question 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

Environment:
Pydantic==2.12.0

Code:

from pydantic import StringConstraints
NonEmptyString = Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)]

Class Test(SQLModel):
    id: NonEmptyString = Field(primary_key=True) # this was working fine on pydantic v2.11.9

Description

Description

After upgrading to Pydantic 2.12.0, SQLModel no longer creates database constraints (like primary keys, unqiue keys ) for fields defined using Annotated types.

Example:

from typing import Annotated
from sqlmodel import SQLModel, Field
from pydantic import StringConstraints

NonEmptyString = Annotated[str, StringConstraints()]

class Test(SQLModel, table=True):
    id: NonEmptyString = Field(primary_key=True)

Expected Behavior

The table should be created with id as an varchar and primary key

Actual Behavior

it throws an error

sqlalchemy.exc.ArgumentError: Mapper Mapper[Test(test)] could not assemble any primary key columns for mapped table 'test'

Additional notes

In case you use an unique key constraint the table will be created, you will not get any error but the unique key constraint will not be created.

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.26

Python Version

3.13.3

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions