1.0
This commit is contained in:
@@ -7,6 +7,7 @@ from app.models.message import Message
|
||||
from app.models.friend import Friend
|
||||
from app.models.friend_request import FriendRequest
|
||||
from app.models.system_config import SystemConfig
|
||||
from app.models.moment import Moment, MomentLike, MomentComment
|
||||
|
||||
__all__ = [
|
||||
"User",
|
||||
@@ -16,4 +17,7 @@ __all__ = [
|
||||
"Friend",
|
||||
"FriendRequest",
|
||||
"SystemConfig",
|
||||
"Moment",
|
||||
"MomentLike",
|
||||
"MomentComment",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
"""朋友圈动态模型"""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import String, Text, DateTime, ForeignKey, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.database import Base
|
||||
|
||||
|
||||
class Moment(Base):
|
||||
__tablename__ = "moments"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"))
|
||||
content: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
images: Mapped[str | None] = mapped_column(Text, nullable=True) # JSON array of URLs
|
||||
visibility: Mapped[str] = mapped_column(String(20), default="friends") # public/friends/private
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.utcnow())
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
default=lambda: datetime.utcnow(),
|
||||
onupdate=lambda: datetime.utcnow(),
|
||||
)
|
||||
|
||||
# 关系
|
||||
user = relationship("User", foreign_keys=[user_id])
|
||||
likes = relationship("MomentLike", back_populates="moment", cascade="all, delete-orphan")
|
||||
comments = relationship("MomentComment", back_populates="moment", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class MomentLike(Base):
|
||||
__tablename__ = "moment_likes"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("moment_id", "user_id", name="uq_moment_like"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
moment_id: Mapped[str] = mapped_column(String(36), ForeignKey("moments.id", ondelete="CASCADE"))
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.utcnow())
|
||||
|
||||
moment = relationship("Moment", back_populates="likes")
|
||||
user = relationship("User", foreign_keys=[user_id])
|
||||
|
||||
|
||||
class MomentComment(Base):
|
||||
__tablename__ = "moment_comments"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
moment_id: Mapped[str] = mapped_column(String(36), ForeignKey("moments.id", ondelete="CASCADE"))
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"))
|
||||
content: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
reply_to_id: Mapped[str | None] = mapped_column(
|
||||
String(36), ForeignKey("moment_comments.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.utcnow())
|
||||
|
||||
moment = relationship("Moment", back_populates="comments")
|
||||
user = relationship("User", foreign_keys=[user_id])
|
||||
reply_to = relationship("MomentComment", remote_side=[id])
|
||||
@@ -13,6 +13,7 @@ class User(Base):
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
username: Mapped[str] = mapped_column(String(50), unique=True, nullable=False, index=True)
|
||||
nickname: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
|
||||
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
avatar_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
|
||||
Reference in New Issue
Block a user