添加数据库级级联删除

This commit is contained in:
haotian 2025-05-26 11:49:10 +08:00
parent 7bcf10357f
commit b2a481ff30
3 changed files with 11 additions and 8 deletions

View File

@ -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

View File

@ -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='创建时间')

View File

@ -184,7 +184,7 @@ async def main():
print("开始数据库测试...")
# 初始化数据库
# await init_db()
await init_db()
print("数据库初始化完成")
# 测试创建