32 lines
853 B
Python
32 lines
853 B
Python
from typing import TYPE_CHECKING
|
|
|
|
from sqlmodel import Field, Relationship, UniqueConstraint
|
|
|
|
from app.models.base import BaseModel
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.sound import Sound
|
|
from app.models.user import User
|
|
|
|
|
|
class SoundPlayed(BaseModel, table=True):
|
|
"""Database model for a sound played."""
|
|
|
|
__tablename__ = "sound_played" # pyright: ignore[reportAssignmentType]
|
|
|
|
user_id: int = Field(foreign_key="user.id", nullable=False)
|
|
sound_id: int = Field(foreign_key="sound.id", nullable=False)
|
|
|
|
# constraints
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"user_id",
|
|
"sound_id",
|
|
name="uq_sound_played_user_sound",
|
|
),
|
|
)
|
|
|
|
# relationships
|
|
user: "User" = Relationship(back_populates="sounds_played")
|
|
sound: "Sound" = Relationship(back_populates="play_history")
|