Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlmodel/ext/asyncio/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async def exec(
```
"""
)
async def execute( # type: ignore
async def execute(
self,
statement: _Executable,
params: Optional[_CoreAnyExecuteParams] = None,
Expand Down
26 changes: 25 additions & 1 deletion sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,31 @@ def get_config(name: str) -> Any:
# If it was passed by kwargs, ensure it's also set in config
set_config_value(model=new_cls, parameter="table", value=config_table)
for k, v in get_model_fields(new_cls).items():
col = get_column_from_field(v)
if not IS_PYDANTIC_V2:
col = get_column_from_field(v)
setattr(new_cls, k, col)
continue

original_field = getattr(v, "_original_assignment", Undefined)
# Get the original sqlmodel FieldInfo, pydantic >=v2.12 changes the model
if isinstance(original_field, FieldInfo):
field = original_field
else:
annotated_field = next(
ann
for c in new_cls.__mro__
if (ann := get_annotations(c.__dict__).get(k)) # type: ignore[arg-type]
)
annotated_field_meta = getattr(
annotated_field, "__metadata__", (())
)
field = next(
(f for f in annotated_field_meta if isinstance(f, FieldInfo)),
v,
) # type: ignore[assignment]
field.annotation = v.annotation
# Guarantee the field has the correct type
col = get_column_from_field(field)
setattr(new_cls, k, col)
# Set a config flag to tell FastAPI that this should be read with a field
# in orm_mode instead of preemptively converting it to a dict.
Expand Down
2 changes: 1 addition & 1 deletion sqlmodel/orm/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def exec(
""",
category=None,
)
def execute( # type: ignore
def execute(
self,
statement: _Executable,
params: Optional[_CoreAnyExecuteParams] = None,
Expand Down
26 changes: 26 additions & 0 deletions tests/test_declaration_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from sqlmodel import Field, SQLModel
from typing_extensions import Annotated


def test_declaration_syntax_1():
class Person1(SQLModel):
name: str = Field(primary_key=True)

class Person1Final(Person1, table=True):
pass


def test_declaration_syntax_2():
class Person2(SQLModel):
name: Annotated[str, Field(primary_key=True)]

class Person2Final(Person2, table=True):
pass


def test_declaration_syntax_3():
class Person3(SQLModel):
name: Annotated[str, ...] = Field(primary_key=True)

class Person3Final(Person3, table=True):
pass
Loading