19 lines
442 B
Python
19 lines
442 B
Python
import re
|
|
from typing import Any
|
|
|
|
from sqlalchemy.ext.declarative import declared_attr
|
|
from sqlalchemy.orm import as_declarative
|
|
|
|
|
|
@as_declarative()
|
|
class Base:
|
|
id: Any
|
|
__name__: str
|
|
|
|
# Generate __tablename__ automatically based on class name
|
|
@declared_attr
|
|
def __tablename__(self) -> str:
|
|
# Convert CamelCase to snake_case
|
|
name = re.sub(r"(?<!^)(?=[A-Z])", "_", self.__name__).lower()
|
|
return name
|