feat: 初始化工程
This commit is contained in:
41
models/base.py
Normal file
41
models/base.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from sqlalchemy import Column, BigInteger, DateTime, event
|
||||
from sqlalchemy.ext.declarative import declared_attr
|
||||
|
||||
from config.database import Base
|
||||
from datetime import datetime
|
||||
|
||||
from utils.common import camel_to_snake
|
||||
from id_generator import options, generator
|
||||
|
||||
# https://github.com/yitter/IdGenerator/tree/master/Python
|
||||
options = options.IdGeneratorOptions(worker_id=23)
|
||||
idgen = generator.DefaultIdGenerator()
|
||||
idgen.set_id_generator(options)
|
||||
|
||||
|
||||
# 第二层基类:包含ID
|
||||
class IdBase(Base):
|
||||
__abstract__ = True
|
||||
|
||||
id = Column(BigInteger, primary_key=True, index=True)
|
||||
|
||||
@declared_attr
|
||||
def __tablename__(cls):
|
||||
# 自动把数据库实体类名驼峰转为数据库表名下划线
|
||||
return camel_to_snake(cls.__name__)
|
||||
|
||||
|
||||
# 自动填充id
|
||||
@event.listens_for(IdBase, 'before_insert', propagate=True)
|
||||
def before_insert_listener(mapper, connection, target):
|
||||
if target.id is None:
|
||||
target.id = idgen.next_id()
|
||||
|
||||
|
||||
# 第二层基类:包含ID和审计字段
|
||||
class AuditBase(IdBase):
|
||||
__abstract__ = True
|
||||
|
||||
create_time = Column(DateTime, nullable=True, default=datetime.now)
|
||||
update_time = Column(DateTime, nullable=True, default=datetime.now, onupdate=datetime.now)
|
||||
|
||||
41
models/family.py
Normal file
41
models/family.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from sqlalchemy import Column, JSON, String, Text, Integer, BigInteger, ForeignKey
|
||||
from sqlalchemy.orm import relationship, Mapped
|
||||
|
||||
from models.base import AuditBase
|
||||
|
||||
|
||||
class Users(AuditBase):
|
||||
name = Column(String(45), nullable=False, comment="昵称")
|
||||
avatar = Column(String(255), nullable=True, comment="头像")
|
||||
|
||||
records: Mapped[list["Records"]] = relationship("Records", back_populates="user")
|
||||
|
||||
|
||||
class Records(AuditBase):
|
||||
user_id = Column(BigInteger, ForeignKey("users.id"), nullable=False, comment="用户id")
|
||||
content = Column(Text, nullable=False, comment="内容")
|
||||
image_list = Column(JSON, nullable=True, default=list, comment="图片")
|
||||
video_list = Column(JSON, nullable=True, default=list, comment="视频")
|
||||
like_count = Column(Integer, nullable=True, default=0, comment="点赞数")
|
||||
comment_count = Column(Integer, nullable=True, default=0, comment="评论数")
|
||||
|
||||
user: Mapped["Users"] = relationship("Users", back_populates="records")
|
||||
comments: Mapped[list["Comments"]] = relationship(
|
||||
"Comments",
|
||||
back_populates="record",
|
||||
lazy="selectin",
|
||||
)
|
||||
|
||||
|
||||
class Likes(AuditBase):
|
||||
record_id = Column(BigInteger, ForeignKey("records.id"), nullable=False, comment="记录id")
|
||||
user_id = Column(BigInteger, ForeignKey("users.id"), nullable=False, comment="用户id")
|
||||
|
||||
|
||||
class Comments(AuditBase):
|
||||
record_id = Column(BigInteger, ForeignKey("records.id"), nullable=False, comment="记录id")
|
||||
user_id = Column(BigInteger, ForeignKey("users.id"), nullable=False, comment="用户id")
|
||||
content = Column(String(255), nullable=False, comment="内容")
|
||||
|
||||
record: Mapped["Records"] = relationship("Records", back_populates="comments")
|
||||
user: Mapped["Users"] = relationship("Users") # ← 就加了这一行
|
||||
Reference in New Issue
Block a user