一、环境:pyqt5+python 3.7+SQLAlchemy 1.4.36+mysql 8.0
二、相关表结构如下:
class maintenanceZone(Base):
__tablename__ = "maintenanceZone" # 区域信息
__table_args__ = {
"mysql_engine": "InnoDB",
}
id = sa.Column(sa.Integer(), primary_key=True, autoincrement=True)
cityName = sa.Column(sa.String(100), nullable=False )
countyName = sa.Column(sa.String(100), nullable=False)
class maintainerInfo(Base):
__tablename__ = "maintainerInfo" #人员信息
__table_args__ = {
"mysql_engine": "InnoDB",
}
id = sa.Column(sa.Integer(), primary_key=True, autoincrement=True)
userName = sa.Column(sa.String(100), unique=True, nullable=False, comment="人员姓名")
userPhone = sa.Column(sa.String(20), unique=True, comment="人员联系电话")
zoneID = sa.Column(sa.Integer(),
sa.ForeignKey("maintenanceZone.id", ondelete="RESTRICT",onupdate="CASCADE"), comment="区域ID")
zoneinfo = relationship("maintenanceZone", backref=backref('maintainerInfo',order_by=id))
三、相关代码如下:
zoneOne = session.query(maintenanceZone).filter(maintenanceZone.cityName == old_city,maintenanceZone.countyName == old_country).one()
session.delete(zoneOne)
session.commit()
四、数据库engine代码如下:
dburl = "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(dbIni["user"], dbIni["password"],dbIni["host"], dbIni["port"], dbIni["db"])
engine = create_engine(
url=dburl,
max_overflow=0,
pool_size=5,
pool_timeout=20,
pool_recycle=-1,
)
五、问题如下:
1、在mysql8.0中,通过sql语删除maintenanceZone表中记录时,会触发外键约束,提示无法删除;
2、在python代码中通过SQLAlchemy删除maintenanceZone表中同一记录时,没有任何提示,直接删除!
SQLAlchemy删除maintenanceZone表记录时,外键约束未何未生效?????