41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
from sqlalchemy import Column, Integer, String, DateTime
|
|
from sqlalchemy.sql import func
|
|
from sqlalchemy.orm import relationship
|
|
from app.db.database import Base
|
|
from pydantic import BaseModel
|
|
|
|
class User(Base):
|
|
"""用户数据模型"""
|
|
__tablename__ = "users"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
|
|
openid = Column(String(50), unique=True, index=True)
|
|
unionid = Column(String(50), nullable=True, index=True)
|
|
avatar = Column(String(255), nullable=True, comment="头像")
|
|
nickname = Column(String(50), nullable=True, comment="昵称")
|
|
tryon_remain_count = Column(Integer, default=0, nullable=False, comment="剩余试穿次数")
|
|
user_code = Column(String(20), unique=True, nullable=True, index=True, comment="用户唯一邀请码")
|
|
referral_code = Column(String(20), nullable=True, index=True, comment="推荐人邀请码")
|
|
create_time = Column(DateTime, default=func.now(), comment="创建时间")
|
|
|
|
# 关系
|
|
person_images = relationship("PersonImage", back_populates="user", cascade="all, delete-orphan")
|
|
clothings = relationship("Clothing", back_populates="user", cascade="all, delete-orphan")
|
|
# tryon_histories已通过backref在TryonHistory模型中定义
|
|
|
|
def __repr__(self):
|
|
return f"<User(id={self.id}, nickname={self.nickname})>"
|
|
|
|
def to_dict(self):
|
|
"""将模型转换为字典"""
|
|
return {
|
|
"id": self.id,
|
|
"openid": self.openid,
|
|
"unionid": self.unionid,
|
|
"avatar": self.avatar,
|
|
"nickname": self.nickname,
|
|
"tryon_remain_count": self.tryon_remain_count,
|
|
"user_code": self.user_code,
|
|
"referral_code": self.referral_code,
|
|
"create_time": self.create_time
|
|
} |