PHP / MySQL - 完整性约束违规:1451不能>删除或更新父行

时间:2018-01-03 02:15:40

标签: mysql foreign-keys

我认为这个问题可能与KEYs或TRIGGERS有关,但不确定从未玩过TRIGGERS等。

我从PHP脚本返回的错误是:

  

错误:SQLSTATE [23000]:完整性约束违规:1451不能   删除或更新父行:外键约束失败   (ambcom_trackingadmins,CONSTRAINT fk_userid_admins FOREIGN KEY   (userid)参考membersid)ON UPDATE CASCADE)

我还尝试手动删除成员表中的用户并获得相同的内容,以便将其缩小到表之间的某些内容。

我从桌子上拿了一个转储,我认为可能是问题所以你可以看到KEY / TRIGGERS等。

-- phpMyAdmin SQL Dump
-- version 4.7.3
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Jan 03, 2018 at 02:13 AM
-- Server version: 5.6.38
-- PHP Version: 5.6.30

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION;


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */;

--
-- Database: ambcom_tracking
--

-- --------------------------------------------------------

--
-- Table structure for table admins
--

CREATE TABLE admins (   adminid char(23) NOT NULL DEFAULT 'uuid_short();',   userid char(23) NOT NULL,   active tinyint(1) NOT NULL DEFAULT '0',   superadmin tinyint(1) NOT NULL DEFAULT '0' ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table cookies
--

CREATE TABLE cookies (   cookieid char(23) NOT NULL,   userid char(23) NOT NULL,   tokenid char(25) NOT NULL,   expired tinyint(1) NOT NULL DEFAULT '0',   timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table members
--

CREATE TABLE members (   id char(23) NOT NULL,   username varchar(65) NOT NULL DEFAULT '',   password varchar(255) NOT NULL DEFAULT '',   email varchar(65) NOT NULL DEFAULT '',   verified tinyint(1) NOT NULL DEFAULT '0',   admin tinyint(1) NOT NULL DEFAULT '0',   mod_timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Triggers members
-- DELIMITER $$ CREATE TRIGGER `add_admin` AFTER INSERT ON `members` FOR EACH ROW BEGIN IF (NEW.admin = 1) THEN  INSERT INTO admins (adminid, userid, active, superadmin ) VALUES (uuid_short(), NEW.id, 1, 0 ); END IF; END $$ DELIMITER ; DELIMITER $$ CREATE TRIGGER `add_admin_beforeUpdate` BEFORE UPDATE ON `members` FOR EACH ROW BEGIN set @s = (SELECT superadmin from admins where userid = NEW.id); set @a
= (SELECT adminid from admins where userid = NEW.id); IF (NEW.admin = 1 && isnull(@a)) THEN INSERT INTO admins ( adminid, userid, active, superadmin ) VALUES ( uuid_short(), NEW.id, 1, 0 ); ELSEIF (NEW.admin
= 0) THEN IF (@s = 0) THEN DELETE FROM admins WHERE userid = NEW.id and superadmin = 0; ELSEIF (@s = 1) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Cannot delete superadmin'; END IF; END IF; END $$ DELIMITER ; DELIMITER $$ CREATE TRIGGER `move_to_deleted_members` AFTER DELETE ON `members` FOR EACH ROW BEGIN DELETE FROM deleted_members WHERE deleted_members.id = OLD.id; UPDATE admins SET active = '0' where admins.userid = OLD.id;  INSERT INTO deleted_members ( id, username, password, email, verified, admin) VALUES ( OLD.id, OLD.username, OLD.password, OLD.email, OLD.verified, OLD.admin ); END $$ DELIMITER ;

-- --------------------------------------------------------

--
-- Table structure for table member_info
--

CREATE TABLE member_info (   userid char(23) NOT NULL,   firstname varchar(45) NOT NULL,   lastname varchar(55) DEFAULT NULL,   phone varchar(20) DEFAULT NULL,   address1 varchar(45) DEFAULT NULL,   address2 varchar(45) DEFAULT NULL,   city varchar(45) DEFAULT NULL,   state varchar(30) DEFAULT NULL,   country varchar(45) DEFAULT NULL,   bio varchar(20000) DEFAULT NULL,   userimage varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table tokens
--

CREATE TABLE tokens (   tokenid char(25) NOT NULL,   userid char(23) NOT NULL,   expired tinyint(1) NOT NULL DEFAULT '0',   timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indexes for dumped tables
--

--
-- Indexes for table admins
-- ALTER TABLE admins   ADD PRIMARY KEY (adminid,userid),   ADD UNIQUE KEY adminid_UNIQUE (adminid),   ADD UNIQUE KEY userid_UNIQUE (userid);

--
-- Indexes for table cookies
-- ALTER TABLE cookies   ADD PRIMARY KEY (userid);

--
-- Indexes for table members
-- ALTER TABLE members   ADD PRIMARY KEY (id),   ADD UNIQUE KEY username_UNIQUE (username),   ADD UNIQUE KEY id_UNIQUE (id),   ADD UNIQUE KEY email_UNIQUE (email);

--
-- Indexes for table member_info
-- ALTER TABLE member_info   ADD UNIQUE KEY userid_UNIQUE (userid),   ADD KEY fk_userid_idx (userid);

--
-- Indexes for table tokens
-- ALTER TABLE tokens   ADD PRIMARY KEY (tokenid),   ADD UNIQUE KEY tokenid_UNIQUE (tokenid),   ADD UNIQUE KEY userid_UNIQUE (userid);

--
-- Constraints for dumped tables
--

--
-- Constraints for table admins
-- ALTER TABLE admins   ADD CONSTRAINT fk_userid_admins FOREIGN KEY (userid) REFERENCES members (id) ON UPDATE CASCADE;

--
-- Constraints for table cookies
-- ALTER TABLE cookies   ADD CONSTRAINT userid FOREIGN KEY (userid) REFERENCES members (id) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table member_info
-- ALTER TABLE member_info   ADD CONSTRAINT fk_userid FOREIGN KEY (userid) REFERENCES members (id) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table tokens
-- ALTER TABLE tokens   ADD CONSTRAINT userid_t FOREIGN KEY (userid) REFERENCES members (id) ON DELETE CASCADE ON UPDATE CASCADE;

DELIMITER $$
--
-- Events
-- CREATE DEFINER=root@localhost EVENT cleanupOldDeleted ON SCHEDULE EVERY 1 DAY STARTS '2017-03-20 18:33:40' ON COMPLETION NOT PRESERVE ENABLE COMMENT 'Removes deleted records older than 30 days.' DO BEGIN DELETE FROM deleted_members WHERE mod_timestamp < DATE_SUB(NOW(), INTERVAL 30 DAY); END$$

DELIMITER ; COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

1 个答案:

答案 0 :(得分:-2)

无法删除,但您要删除哪一个? 你必须更具体。
根据您的设计,我假设您尝试DELETE FROM members记录。

members是您的表格参考,除非您先删除子广告(admins),否则无法将其删除。