2025-06-05 05:49:19 +00:00

37 lines
1.3 KiB
Python

from sqlalchemy import Column, String, Integer, ForeignKey, Float, Table
from sqlalchemy.orm import relationship
from app.db.session import Base
from app.models.base import BaseModel
# Association table for the many-to-many relationship between songs and playlists
song_playlist = Table(
"song_playlist",
Base.metadata,
Column("song_id", Integer, ForeignKey("songs.id"), primary_key=True),
Column("playlist_id", Integer, ForeignKey("playlists.id"), primary_key=True),
)
class Song(BaseModel):
"""Song model representing an audio track."""
__tablename__ = "songs"
title = Column(String, nullable=False, index=True)
duration = Column(Float, nullable=True) # Duration in seconds
file_path = Column(String, nullable=False) # Path to audio file
track_number = Column(Integer, nullable=True) # Track number in the album
# Foreign keys
artist_id = Column(Integer, ForeignKey("artists.id"), nullable=False)
album_id = Column(Integer, ForeignKey("albums.id"), nullable=True)
# Relationships
artist = relationship("Artist", back_populates="songs")
album = relationship("Album", back_populates="songs")
playlists = relationship("Playlist", secondary=song_playlist, back_populates="songs")
def __repr__(self):
return f"<Song {self.title}>"