I am setting up a database and trying my hand at implementing triggers. All Tables and the trigger upload into the database without warning, however when I activate the trigger on update of the table operator I get an error message
UPDATE operator SET `NAME_FIRST`='JOHN' WHERE `OPERATOR_ID`=0;
SQL Error (1109): Unknown table 'NEW' in field list
I suspect that it is in the Auditor Triggers.
Database export with HEIDISQL
-- --------------------------------------------------------
-- Host: 127.0.0.1
-- Server version: 8.0.32 - MySQL Community Server - GPL
-- Server OS: Win64
-- HeidiSQL Version: 12.8.0.6908
-- --------------------------------------------------------
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- Dumping structure for table test.auditor
CREATE TABLE IF NOT EXISTS `auditor` (
`OPERATOR_ID` int NOT NULL,
`IS_ACTIVE` enum('TRUE','FALSE') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'TRUE',
PRIMARY KEY (`OPERATOR_ID`),
CONSTRAINT `auditor_FK_OPERATOR_ID` FOREIGN KEY (`OPERATOR_ID`) REFERENCES `operator` (`OPERATOR_ID`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Dumping data for table test.auditor: ~0 rows (approximately)
-- Dumping structure for procedure test.auditor_validation
DELIMITER //
CREATE PROCEDURE `auditor_validation`(
IN `OPERATOR_ID` INT
)
BEGIN
DECLARE myVar VARCHAR(5);
SELECT
`IS_ACTIVE` INTO myVar
FROM
operator
WHERE
operator.OPERATOR_ID = OPERATOR_ID;
IF NEW.ACTIVE = 'TRUE' AND myVar = 'FALSE' THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Operator is marked as inactive';
END IF;
END//
DELIMITER ;
-- Dumping structure for table test.operator
CREATE TABLE IF NOT EXISTS `operator` (
`OPERATOR_ID` int NOT NULL AUTO_INCREMENT,
`NAME_FIRST` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`NAME_LAST` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`BUSINESS_ID` char(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`IS_ACTIVE` enum('TRUE','FALSE') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'TRUE',
PRIMARY KEY (`OPERATOR_ID`),
KEY `BUSINESS_ID` (`BUSINESS_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Dumping data for table test.operator: ~2 rows (approximately)
INSERT INTO `operator` (`OPERATOR_ID`, `NAME_FIRST`, `NAME_LAST`, `BUSINESS_ID`, `IS_ACTIVE`) VALUES
(-1, 'JANE', 'DOE', '456XYZ', 'TRUE'),
(2, 'JOE', 'DOE', 'ABC123', 'FALSE');
-- Dumping structure for trigger test.auditor_before_insert
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
DELIMITER //
CREATE TRIGGER `auditor_before_insert` BEFORE INSERT ON `auditor` FOR EACH ROW BEGIN
CALL auditor_validation(NEW.OPERATOR_ID);
END//
DELIMITER ;
SET SQL_MODE=@OLDTMP_SQL_MODE;
-- Dumping structure for trigger test.auditor_before_update
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
DELIMITER //
CREATE TRIGGER `auditor_before_update` BEFORE UPDATE ON `auditor` FOR EACH ROW BEGIN
CALL auditor_validation(NEW.OPERATOR_ID);
END//
DELIMITER ;
SET SQL_MODE=@OLDTMP_SQL_MODE;
-- Dumping structure for trigger test.operator_after_update
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
DELIMITER //
CREATE TRIGGER `operator_after_update` AFTER UPDATE ON `operator` FOR EACH ROW BEGIN
DECLARE OP_ID INT;
SET OP_ID = NEW.OPERATOR_ID;
IF NEW.IS_ACTIVE = 'FALSE' THEN
UPDATE auditor
SET auditor.IS_ACTIVE = 'FALSE'
WHERE auditor.OPERATOR_ID = OP_ID;
END IF;
END//
DELIMITER ;
SET SQL_MODE=@OLDTMP_SQL_MODE;
/*!40103 SET TIME_ZONE=IFNULL(@OLD_TIME_ZONE, 'system') */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */;
I have tried replacing the NOW.%Value%
in the Triggers but that would sometimes stop the error from showing up but not always
EDIT: the error shows up with insert into auditor (`Operator_ID`,`IS_ACTIVE`) VALUES (2, 'FALSE');
as well.
apparently the error only shows when the auditor table gets changed or a parent of an auditor record gets changed, definitely points to the procedure
4
I found the NEW
reference that was causing the issue, as part of the auditor_validation procedure, changing that into a parameter fixed the issue and allows insertions to the auditor table and updates to the operator table