I keep getting errors when importing my old SQL file and fix them all,but I'm stuck and can't understand what this means.
ALTER TABLE
property
ADD CONSTRAINTproperty_ibfk_1
FOREIGN KEY (intid
) REFERENCESinteriors
(id
) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINTproperty_ibfk_2
FOREIGN KEY (owner
) REFERENCESaccounts
(id
) ON DELETE SET NULL ON UPDATE CASCADE MySQL said: Documentation1452 - Cannot add or update a child row: a foreign key constraint fails (
ionicnew
.#sql-252c_e1
, CONSTRAINTproperty_ibfk_2
FOREIGN KEY (owner
) REFERENCESaccounts
(id
) ON DELETE SET NULL ON UPDATE CASCADE)
Full code of property
table:
CREATE TABLE `property` (
`id` int(11) NOT NULL,
`x` float NOT NULL,
`y` float NOT NULL,
`z` float NOT NULL,
`a` float NOT NULL,
`type` bit(32) NOT NULL,
`intid` int(11) NOT NULL,
`name` varchar(128) NOT NULL,
`price` int(11) NOT NULL,
`mapicon` tinyint(3) UNSIGNED NOT NULL,
`status` tinyint(3) UNSIGNED NOT NULL,
`point` int(10) UNSIGNED NOT NULL,
`saleprice` int(11) NOT NULL DEFAULT '0',
`owner` int(11) DEFAULT NULL,
`money` int(11) NOT NULL DEFAULT '0',
`level` tinyint(3) UNSIGNED NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `property`
ADD PRIMARY KEY (`id`),
ADD KEY `intid` (`intid`),
ADD KEY `owner` (`owner`);
ALTER TABLE `property`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=86;
ALTER TABLE `property`
ADD CONSTRAINT `property_ibfk_1` FOREIGN KEY (`intid`) REFERENCES `interiors` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `property_ibfk_2` FOREIGN KEY (`owner`) REFERENCES `accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;
I can upload the full SQL file if needed.
So your error
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails
essentially means that, you are trying to add a row to yourproperty
table for which no matching row(intid)
is present ininteriors
table.You must first insert the row to your
interiors
table.