24 lines
1009 B
Python
24 lines
1009 B
Python
from datetime import datetime
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, Integer, String, UniqueConstraint, func
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.db.base import Base
|
|
|
|
|
|
class Rating(Base):
|
|
__tablename__ = "ratings"
|
|
__table_args__ = (UniqueConstraint("user_id", "spot_id", name="uq_user_spot_rating"),)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
spot_id: Mapped[int] = mapped_column(Integer, ForeignKey("spots.id"), nullable=False, index=True)
|
|
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False)
|
|
score: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
short_comment: Mapped[str | None] = mapped_column(String(200))
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
user = relationship("User", lazy="selectin")
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<Rating {self.id} spot={self.spot_id} score={self.score}>"
|