Skip to content
Discussion options

You must be logged in to vote
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 CropMask(SQLModelBase, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)

engine = create_engine("sqlite:///", echo=True)
SQLModel.metadata.create_all(engine)

Output:

CREATE TABLE crop_mask (
        id INTEGER NOT NULL, 
        PRIMARY KEY (id)
)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants