34 lines
1.3 KiB
Python
34 lines
1.3 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="昵称")
|
|
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")
|
|
|
|
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,
|
|
"create_time": self.create_time
|
|
} |