Skip to content
Closed
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 scripts/generate_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Arg(BaseModel):
else:
t_type = f"_T{i}"
t_var = f"_TCCA[{t_type}]"
arg = Arg(name=f"__ent{i}", annotation=t_var)
arg = Arg(name=f"ent{i}", annotation=t_var)
ret_type = t_type
args.append(arg)
return_types.append(ret_type)
Expand Down
22 changes: 13 additions & 9 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from sqlalchemy.orm import (
Mapped,
RelationshipProperty,
declared_attr,
registry,
relationship,
)
Expand All @@ -50,6 +49,7 @@
from sqlalchemy.orm.instrumentation import is_instrumented
from sqlalchemy.sql.schema import MetaData
from sqlalchemy.sql.sqltypes import LargeBinary, Time, Uuid
from sqlalchemy.types import TypeEngine
from typing_extensions import deprecated

from ._compat import ( # type: ignore[attr-defined]
Expand Down Expand Up @@ -209,7 +209,7 @@ class FieldInfoMetadata:
ondelete: OnDeleteType | UndefinedType = Undefined
unique: bool | UndefinedType = Undefined
index: bool | UndefinedType = Undefined
sa_type: type[Any] | UndefinedType = Undefined
sa_type: type[Any] | TypeEngine[Any] | UndefinedType = Undefined
sa_column: Column[Any] | UndefinedType = Undefined
sa_column_args: Sequence[Any] | UndefinedType = Undefined
sa_column_kwargs: Mapping[str, Any] | UndefinedType = Undefined
Expand Down Expand Up @@ -268,7 +268,7 @@ def Field(
unique: bool | UndefinedType = Undefined,
nullable: bool | UndefinedType = Undefined,
index: bool | UndefinedType = Undefined,
sa_type: type[Any] | UndefinedType = Undefined,
sa_type: type[Any] | TypeEngine[Any] | UndefinedType = Undefined,
sa_column_args: Sequence[Any] | UndefinedType = Undefined,
sa_column_kwargs: Mapping[str, Any] | UndefinedType = Undefined,
schema_extra: dict[str, Any] | None = None,
Expand Down Expand Up @@ -312,7 +312,7 @@ def Field(
unique: bool | UndefinedType = Undefined,
nullable: bool | UndefinedType = Undefined,
index: bool | UndefinedType = Undefined,
sa_type: type[Any] | UndefinedType = Undefined,
sa_type: type[Any] | TypeEngine[Any] | UndefinedType = Undefined,
sa_column_args: Sequence[Any] | UndefinedType = Undefined,
sa_column_kwargs: Mapping[str, Any] | UndefinedType = Undefined,
schema_extra: dict[str, Any] | None = None,
Expand Down Expand Up @@ -397,7 +397,7 @@ def Field(
unique: bool | UndefinedType = Undefined,
nullable: bool | UndefinedType = Undefined,
index: bool | UndefinedType = Undefined,
sa_type: type[Any] | UndefinedType = Undefined,
sa_type: type[Any] | TypeEngine[Any] | UndefinedType = Undefined,
sa_column: Column | UndefinedType = Undefined, # type: ignore
sa_column_args: Sequence[Any] | UndefinedType = Undefined,
sa_column_kwargs: Mapping[str, Any] | UndefinedType = Undefined,
Expand Down Expand Up @@ -566,6 +566,14 @@ def __new__(
"__sqlmodel_relationships__": relationships,
"__annotations__": pydantic_annotations,
}
# Set default __tablename__ before class creation so it's part of the
# class dict rather than assigned through the class object afterwards.
is_table = (
kwargs.get("table") is True
or (class_dict.get("model_config") or {}).get("table") is True
)
if is_table and "__tablename__" not in class_dict:
dict_used["__tablename__"] = name.lower()
# Duplicate logic from Pydantic to filter config kwargs because if they are
# passed directly including the registry Pydantic will pass them over to the
# superclass causing an error
Expand Down Expand Up @@ -865,10 +873,6 @@ def __repr_args__(self) -> Sequence[tuple[str | None, Any]]:
if not (isinstance(k, str) and k.startswith("_sa_"))
]

@declared_attr # type: ignore
def __tablename__(cls) -> str:
return cls.__name__.lower()

@classmethod
def model_validate( # type: ignore[override]
cls: type[_TSQLModel],
Expand Down
Loading
Loading