如何解决这个问题 - 无法删除或更新父行:外键约束失败
Posted
技术标签:
【中文标题】如何解决这个问题 - 无法删除或更新父行:外键约束失败【英文标题】:how to solve this- Cannot delete or update a parent row: a foreign key constraint fails 【发布时间】:2013-03-04 14:23:26 【问题描述】:我正在尝试删除表并收到此错误:
无法删除或更新父行:外键约束失败
有些人可以帮助我:
声明:DROP TABLE vehiculo
错误:#1217 - Cannot delete or update a parent row: a foreign key constraint fails
与vehiculo有关系的表:
CREATE TABLE `vehiculo` (
`numero_movil` int(3) unsigned NOT NULL,
`numeroChasis` varchar(30) COLLATE utf8_spanish2_ci NOT NULL,
`numeroMotor` varchar(30) COLLATE utf8_spanish2_ci NOT NULL,
`Marca` varchar(15) COLLATE utf8_spanish2_ci NOT NULL,
`Modelo` smallint(5) unsigned NOT NULL,
`Color` varchar(10) COLLATE utf8_spanish2_ci NOT NULL,
`Propietario_Cedula` int(11) NOT NULL,
`Cuota` int(11) NOT NULL,
`Turno_idTurno` tinyint(3) unsigned NOT NULL,
`estado_pago_central` int(11) NOT NULL,
`DocumentoVehiculo` int(10) unsigned NOT NULL,
`f_numero_movil` int(10) unsigned NOT NULL,
`f_Propietario_Cedula` int(11) NOT NULL,
`idEstadoPapelesVehiculo` tinyint(3) unsigned NOT NULL,
`placa` varchar(8) COLLATE utf8_spanish2_ci NOT NULL,
PRIMARY KEY (`numero_movil`,`Propietario_Cedula`,`Turno_idTurno`,`estado_pago_central`,`DocumentoVehiculo`,`f_numero_movil`,`f_Propietario_Cedula`,`idEstadoPapelesVehiculo`),
UNIQUE KEY `numeroChasis_UNIQUE` (`numeroChasis`),
UNIQUE KEY `numeroMotor_UNIQUE` (`numeroMotor`),
KEY `fk_Vehiculo_Propietario1_idx` (`Propietario_Cedula`),
KEY `fk_Vehiculo_Turno1_idx` (`Turno_idTurno`),
KEY `fk_Vehiculo_estado_pago_central1_idx` (`estado_pago_central`),
KEY `fk_Vehiculo_DocumentosVehiculo1_idx` (`DocumentoVehiculo`,`f_numero_movil`,`f_Propietario_Cedula`,`idEstadoPapelesVehiculo`),
CONSTRAINT `fk_Vehiculo_DocumentosVehiculo1` FOREIGN KEY (`DocumentoVehiculo`, `f_numero_movil`, `f_Propietario_Cedula`, `idEstadoPapelesVehiculo`) REFERENCES `documentosvehiculo` (`idDocumentoVehiculo`, `Vehiculo_numero_movil`, `Vehiculo_Propietario_Cedula`, `EstadoPapelesVehiculo_idEstadoPapelesVehiculo`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Vehiculo_estado_pago_central1` FOREIGN KEY (`estado_pago_central`) REFERENCES `estado_pago_central` (`idestado`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Vehiculo_Turno1` FOREIGN KEY (`Turno_idTurno`) REFERENCES `turno` (`idTurno`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `vehiculo_ibfk_1` FOREIGN KEY (`Propietario_Cedula`) REFERENCES `propietario` (`Cedula`) ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci
CREATE TABLE `estadopapelesvehiculo` ( `idEstadoPapelesVehiculo` tinyint(3) unsigned NOT NULL, `EstadoPapelesVehiculocol` varchar(45) COLLATE utf8_spanish2_ci NOT NULL, PRIMARY KEY (`idEstadoPapelesVehiculo`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci
CREATE TABLE `propietario` ( `Cedula` int(11) NOT NULL, `apellidos` varchar(30) COLLATE utf8_spanish2_ci NOT NULL, `nombre` varchar(30) COLLATE utf8_spanish2_ci NOT NULL, `fechaNacimiento` date NOT NULL, PRIMARY KEY (`Cedula`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci
【问题讨论】:
有没有使用vehicle表作为外键的表?这通常是尝试删除表并显示该错误时的问题。 【参考方案1】:您有一个表,该表依赖于该表中的数据,并且正在阻止删除或删除该行。如果您发现任何依赖于它的表,您可以删除它们,或删除约束,然后删除车辆表。
【讨论】:
以上是关于如何解决这个问题 - 无法删除或更新父行:外键约束失败的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 关于删除完整性约束违规:1451 无法删除或更新父行:外键约束失败
TypeORM/MySQL:无法删除或更新父行:外键约束失败
Laravel SQLSTATE[23000] 无法删除或更新父行:外键约束失败
Flask中的(pymysql.err.IntegrityError)(1451,'无法删除或更新父行:外键约束失败...')