40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
"""ORM model for likes — an append-only event log.
|
|
|
|
A like/dislike is **never** updated in place. Current state for a ``(user,
|
|
track)`` pair is the latest event by ``created_at``. This shape is required for
|
|
future sync and as a clean ML signal (plan §4.1). Hence: no ``updated_at``,
|
|
no unique constraint on ``(user, track)``.
|
|
"""
|
|
|
|
import datetime as dt
|
|
import uuid
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, Index, String, func
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.infrastructure.db.base import Base
|
|
from app.infrastructure.db.models.mixins import UUIDPrimaryKeyMixin
|
|
|
|
|
|
class LikeModel(UUIDPrimaryKeyMixin, Base):
|
|
__tablename__ = "likes"
|
|
__table_args__ = (
|
|
# Latest-event lookups query by (user, track) ordered by time.
|
|
Index("ix_likes_user_id_track_id", "user_id", "track_id"),
|
|
)
|
|
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
ForeignKey("users.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
track_id: Mapped[uuid.UUID] = mapped_column(
|
|
ForeignKey("tracks.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
value: Mapped[str] = mapped_column(String(16), nullable=False)
|
|
created_at: Mapped[dt.datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
server_default=func.now(),
|
|
nullable=False,
|
|
)
|