"""Flask扩展实例。"""

from __future__ import annotations

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import MetaData
from sqlalchemy.orm import DeclarativeBase


NAMING_CONVENTION = {
    "ix": "ix_%(table_name)s_%(column_0_N_name)s",
    "uq": "uq_%(table_name)s_%(column_0_N_name)s",
    "ck": "ck_%(table_name)s_%(constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_N_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s",
}


class Base(DeclarativeBase):
    """带稳定约束命名规则的声明式基类。"""

    metadata = MetaData(naming_convention=NAMING_CONVENTION)


db = SQLAlchemy(
    model_class=Base,
    session_options={"expire_on_commit": False},
)

__all__ = ["Base", "db"]
