32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
from typing import TYPE_CHECKING
|
|
from sqlalchemy import Column, DateTime, String, ForeignKey, UniqueConstraint
|
|
from sqlalchemy.sql import func
|
|
from .base import AutoID, Base
|
|
|
|
# 只有在静态检查时才导入
|
|
# 运行阶段这段代码会被跳过,彻底避免循环导入
|
|
# 不确定是否用得上,但感觉这是个好习惯,尤其在模型之间有外键关系时
|
|
if TYPE_CHECKING:
|
|
from .user import User
|
|
|
|
class Follow(Base, AutoID):
|
|
"""用户关注关系表"""
|
|
__tablename__ = "follows"
|
|
|
|
follower_id = Column(
|
|
String(User.id_length()),
|
|
ForeignKey("users.id", ondelete="CASCADE"),
|
|
index=True
|
|
)
|
|
followee_id = Column(
|
|
String(User.id_length()),
|
|
ForeignKey("users.id", ondelete="CASCADE"),
|
|
index=True
|
|
)
|
|
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
# 联合唯一索引:防止重复关注
|
|
__table_args__ = (
|
|
UniqueConstraint("follower_id", "followee_id", name="uq_follower_followee"),
|
|
) |