how to set the table_name manually instead of generating from class name? #1421
-
First Check
Commit to Help
Example Codeclass MyTable(SQLModel, table=True):
id: int Descriptionwant the table name to be Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.24 Python Version3.13 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
YuriiMotov
Aug 21, 2025
Replies: 1 comment
-
You can set it manually by class MyTable(SQLModel, table=True):
__tablename__ = "my_table"
... Or specify function that will be used to generate name (useful if you specify it in base class): from typing import Optional
from pydantic.alias_generators import to_snake
from sqlalchemy.orm import declared_attr
from sqlmodel import Field, SQLModel, create_engine
class SQLModelBase(SQLModel):
@declared_attr.directive # type: ignore[misc]
@classmethod
def __tablename__(cls) -> str:
return to_snake(cls.__name__)
class MyTable(SQLModelBase, table=True):
... |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
YuriiMotov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can set it manually by
Or specify function that will be used to generate name (useful if you specify it in base class):