From b2a481ff300b1490b09eff93282e55381485cdf1 Mon Sep 17 00:00:00 2001 From: haotian <2421912570@qq.com> Date: Mon, 26 May 2025 11:49:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E7=BA=A7=E7=BA=A7=E8=81=94=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/config.py | 3 +++ app/models/models.py | 14 +++++++------- test_db.py | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/core/config.py b/app/core/config.py index 25fe352..183d6fd 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -6,7 +6,10 @@ class Settings(BaseSettings): DB_PORT: int = 3306 DB_USER: str = "root" DB_PASSWORD: str = "root" + DB_NAME: str = "kangda" + # DB_NAME: str = "kangda_test" # 测试数据库 + DB_CHARSET: str = "utf8mb4" DB_POOL_SIZE: int = 5 DB_MAX_OVERFLOW: int = 10 diff --git a/app/models/models.py b/app/models/models.py index 8116f29..abb063a 100644 --- a/app/models/models.py +++ b/app/models/models.py @@ -60,16 +60,16 @@ class Event(Base): updateTime = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='本地后台更新时间') # 关系 - images = relationship("Image", back_populates="event", cascade="all, delete-orphan") - temperatures = relationship("Temperature", back_populates="event", cascade="all, delete-orphan") - process_logs = relationship("ProcessLog", back_populates="event", cascade="all, delete-orphan") + images = relationship("Image", back_populates="event", cascade="all, delete", passive_deletes=True) + temperatures = relationship("Temperature", back_populates="event", cascade="all, delete", passive_deletes=True) + process_logs = relationship("ProcessLog", back_populates="event", cascade="all, delete", passive_deletes=True) class Image(Base): __tablename__ = "image" imageId = Column(BigInteger, primary_key=True, autoincrement=True, comment='图片ID') - eventId = Column(String(50), ForeignKey('event.eventId'), nullable=False, comment='关联事件ID') + eventId = Column(String(50), ForeignKey('event.eventId', ondelete="CASCADE"), nullable=False, comment='关联事件ID') imageUrl = Column(String(500), nullable=False, comment='图片URL') localPath = Column(String(500), comment='本地存储路径') createTime = Column(DateTime, default=datetime.now, nullable=False, comment='创建时间') @@ -87,8 +87,8 @@ class Temperature(Base): __tablename__ = "temperature" tempId = Column(BigInteger, primary_key=True, autoincrement=True, comment='温度记录ID') - eventId = Column(String(50), ForeignKey('event.eventId'), nullable=False, comment='关联事件ID') - imageId = Column(BigInteger, ForeignKey('image.imageId'), nullable=False, comment='关联图片ID') + eventId = Column(String(50), ForeignKey('event.eventId', ondelete="CASCADE"), nullable=False, comment='关联事件ID') + imageId = Column(BigInteger, ForeignKey('image.imageId', ondelete="CASCADE"), nullable=False, comment='关联图片ID') temperature = Column(String(20), nullable=False, comment='温度值') status = Column(String(5), comment='温度是否正常') confidence = Column(String(40), nullable=False, comment='识别置信度') @@ -108,7 +108,7 @@ class ProcessLog(Base): __tablename__ = "process_log" logId = Column(BigInteger, primary_key=True, autoincrement=True, comment='日志ID') - eventId = Column(String(50), ForeignKey('event.eventId'), nullable=False, comment='关联事件ID') + eventId = Column(String(50), ForeignKey('event.eventId', ondelete="CASCADE"), nullable=False, comment='关联事件ID') processStatus = Column(Integer, nullable=False, comment='处理状态') errorMessage = Column(Text, comment='错误信息') createTime = Column(DateTime, default=datetime.now, nullable=False, comment='创建时间') diff --git a/test_db.py b/test_db.py index d4f17c1..ea55493 100644 --- a/test_db.py +++ b/test_db.py @@ -184,7 +184,7 @@ async def main(): print("开始数据库测试...") # 初始化数据库 - # await init_db() + await init_db() print("数据库初始化完成") # 测试创建