当我在从数据库中查询现有对象后尝试删除它时,出现此循环依赖错误。我无法弄清楚为什么会发生这种情况以及为什么我可以查询对象并获取所有数据但不能删除它而不会导致此依赖错误。任何帮助或提示将不胜感激。
另一方面:我是否需要 backref 中的级联和 ondelete='CASCADE'?
class Device(db.Model):
__tablename__ = "device"
# Properties
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(32))
serial_nr = db.Column(db.String(32))
model = db.Column(db.String(32))
hersteller = db.Column(db.String(32))
active = db.Column(db.Boolean, default=True)
device_type = db.Column(db.String(32))
__mapper_args__ = {
'polymorphic_identity': 'device',
'polymorphic_on': device_type
}
class NetworkDevice(Device):
__tablename__ = "networkdevice"
__mapper_args__ = {
'polymorphic_identity': 'networkdevice',
}
# Properties
MAC_Lan = db.Column(db.BIGINT)
MAC_Wlan = db.Column(db.BIGINT)
IP = db.Column(db.Integer)
nic_speed = db.Column(db.Integer)
# Relationships
device_id = db.Column(db.Integer, db.ForeignKey(Device.id, ondelete="CASCADE"), primary_key=True)
device = db.relationship("Device", backref=backref("networkdevice2", uselist=False, cascade="all, delete-orphan"))
class Drucker(NetworkDevice):
__tablename__ = "drucker"
__mapper_args__ = {
'polymorphic_identity': 'drucker',
}
# Properties
duplex = db.Column(db.Boolean)
# Relationships
device_id = db.Column(db.Integer, db.ForeignKey(NetworkDevice.device_id, ondelete="CASCADE"), primary_key=True)
networkdevice = db.relationship("NetworkDevice", backref=backref("drucker", uselist=False, cascade="all, delete-orphan"))
query_result = Device.query.filter(Device.id == 321).one_or_none()
if query_result is not None:
db.session.delete(query_result)
db.session.commit()
2021-06-17 11:38:49,878 INFO sqlalchemy.engine.Engine SHOW VARIABLES LIKE 'sql_mode'
2021-06-17 11:38:49,882 INFO sqlalchemy.engine.Engine [raw sql] {}
2021-06-17 11:38:49,885 INFO sqlalchemy.engine.Engine SHOW VARIABLES LIKE 'lower_case_table_names'
2021-06-17 11:38:49,885 INFO sqlalchemy.engine.Engine [generated in 0.00068s] {}
2021-06-17 11:38:49,888 INFO sqlalchemy.engine.Engine SELECT DATABASE()
2021-06-17 11:38:49,888 INFO sqlalchemy.engine.Engine [raw sql] {}
2021-06-17 11:38:49,890 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2021-06-17 11:38:49,894 INFO sqlalchemy.engine.Engine SELECT device.id AS device_id, device.name AS device_name, device.serial_nr AS device_serial_nr, device.model AS device_model, device.hersteller AS device_hersteller, device.active AS device_active, device.device_type AS device_device_type, networkdevice.`MAC_Lan` AS `networkdevice_MAC_Lan`, networkdevice.`MAC_Wlan` AS `networkdevice_MAC_Wlan`, networkdevice.`IP` AS `networkdevice_IP`, networkdevice.nic_speed AS networkdevice_nic_speed, drucker.device_id AS drucker_device_id, networkdevice.device_id AS networkdevice_device_id, drucker.duplex AS drucker_duplex
FROM device INNER JOIN networkdevice ON device.id = networkdevice.device_id INNER JOIN drucker ON networkdevice.device_id = drucker.device_id
WHERE device.id = %(id_1)s
2021-06-17 11:38:49,895 INFO sqlalchemy.engine.Engine [generated in 0.00079s] {'id_1': 321}
2021-06-17 11:38:49,901 INFO sqlalchemy.engine.Engine SELECT device.id AS device_id, device.name AS device_name, device.serial_nr AS device_serial_nr, device.model AS device_model, device.hersteller AS device_hersteller, device.active AS device_active, device.device_type AS device_device_type, networkdevice.`MAC_Lan` AS `networkdevice_MAC_Lan`, networkdevice.`MAC_Wlan` AS `networkdevice_MAC_Wlan`, networkdevice.`IP` AS `networkdevice_IP`, networkdevice.nic_speed AS networkdevice_nic_speed, drucker.device_id AS drucker_device_id, networkdevice.device_id AS networkdevice_device_id, drucker.duplex AS drucker_duplex
FROM device INNER JOIN networkdevice ON device.id = networkdevice.device_id INNER JOIN drucker ON networkdevice.device_id = drucker.device_id
WHERE %(param_1)s = drucker.device_id
2021-06-17 11:38:49,901 INFO sqlalchemy.engine.Engine [generated in 0.00062s] {'param_1': 321}
2021-06-17 11:38:49,904 INFO sqlalchemy.engine.Engine ROLLBACK
CircularDependencyError: Circular dependency detected. (DeleteState(<Drucker at 0x26d01f07b50>))