Files
eryao/backend/src/models/profile.py
T

36 lines
1.2 KiB
Python

from __future__ import annotations
import uuid
from sqlalchemy import CheckConstraint, ForeignKey, Index, JSON, String, Text, text
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
from core.db.base import Base, SoftDeleteMixin, TimestampMixin
class Profile(TimestampMixin, SoftDeleteMixin, Base):
__tablename__ = "profiles"
__table_args__ = (
CheckConstraint(
"char_length(username) >= 1", name="ck_profiles_username_non_empty"
),
Index("ix_profiles_username", "username"),
Index("ix_profiles_settings_gin", "settings", postgresql_using="gin"),
)
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("auth.users.id", ondelete="CASCADE"),
primary_key=True,
)
username: Mapped[str] = mapped_column(String(30), nullable=False)
avatar_url: Mapped[str | None] = mapped_column(Text, nullable=True)
bio: Mapped[str | None] = mapped_column(String(200), nullable=True)
settings: Mapped[dict[str, object]] = mapped_column(
JSON().with_variant(JSONB, "postgresql"),
nullable=False,
server_default=text("'{}'::jsonb"),
default=dict,
)