MySQL Forums
Forum List  »  Triggers

(UPDATED with Schema) Triggers before update error (Error Code: 1054. Unknown column 'workflow_id' in 'NEW')
Posted by: Steve Landry
Date: January 23, 2014 11:24AM

Here's my table schema with sample data:
CREATE DATABASE  IF NOT EXISTS `tsttrigger` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `tsttrigger`;
-- MySQL dump 10.13  Distrib 5.6.13, for Win32 (x86)
--
-- Host: localhost    Database: tsttrigger
-- ------------------------------------------------------
-- Server version	5.6.15

/*!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 utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!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 */;

--
-- Table structure for table `submission`
--

DROP TABLE IF EXISTS `submission`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `submission` (
  `id` int(11) NOT NULL,
  `workflow_id` int(11) NOT NULL,
  `description` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_table1_table2_idx` (`workflow_id`),
  CONSTRAINT `fk_workflow_id` FOREIGN KEY (`workflow_id`) REFERENCES `workflow` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `submission`
--

LOCK TABLES `submission` WRITE;
/*!40000 ALTER TABLE `submission` DISABLE KEYS */;
INSERT INTO `submission` (`id`, `workflow_id`, `description`) VALUES (1,1,'testing status'),(2,3,'new test');
/*!40000 ALTER TABLE `submission` ENABLE KEYS */;
UNLOCK TABLES;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `before_update_submission` BEFORE UPDATE ON `submission` FOR EACH ROW
BEGIN


	DROP TEMPORARY TABLE IF EXISTS tempTbl;
	CREATE TEMPORARY TABLE tempTbl AS 
		SELECT wf.id, wf.allow_status, wf.active
		from workflow as wf
		join submission as sub on wf.id = NEW.workflow_id
		where sub.id = NEW.id
		order by wf.allow_status;
	
	SET @Is_Auto_Count = (SELECT COUNT(active) FROM tempTbl WHERE active = TRUE);

	IF @Is_Auto_Count = 1 THEN

		insert into submission_log (id, workflow_id, description)
		values(NEW.id, NEW.workflow_id, NEW.description);

		SET NEW.workflow_id = (SELECT allow_status FROM tempTbl WHERE active = TRUE);


		insert into submission_log (id, workflow_id, description)
		values(NEW.id, NEW.workflow_id, NEW.description);

	ELSEIF @Is_Auto_Count = 0 THEN

		insert into submission_log (id, workflow_id, description)
		values(NEW.id, NEW.workflow_id, NEW.description);

	ELSEIF @Is_Auto_Count > 1 THEN

		SET @msg = 'There is more than one automated destination for this status';
		SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @msg;

	END IF;
end */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;

--
-- Table structure for table `submission_log`
--

DROP TABLE IF EXISTS `submission_log`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `submission_log` (
  `id` int(11) DEFAULT NULL,
  `workflow_id` int(11) DEFAULT NULL,
  `description` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `submission_log`
--

LOCK TABLES `submission_log` WRITE;
/*!40000 ALTER TABLE `submission_log` DISABLE KEYS */;
/*!40000 ALTER TABLE `submission_log` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `workflow`
--

DROP TABLE IF EXISTS `workflow`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `workflow` (
  `id` int(11) NOT NULL,
  `allow_status` int(11) DEFAULT NULL,
  `active` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `workflow`
--

LOCK TABLES `workflow` WRITE;
/*!40000 ALTER TABLE `workflow` DISABLE KEYS */;
INSERT INTO `workflow` (`id`, `allow_status`, `active`) VALUES (1,2,0),(2,3,1),(3,4,0),(4,5,0),(5,6,1),(6,99,0);
/*!40000 ALTER TABLE `workflow` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Dumping events for database 'tsttrigger'
--

--
-- Dumping routines for database 'tsttrigger'
--
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!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 */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2014-01-24 11:59:49

If you run the following update statement you'll get the error on the second automated status:

FLUSH TABLES;
truncate tsttrigger.submission_log;
select * from tsttrigger.submission;
select * from tsttrigger.workflow;

-- Id 1 not automated to next status 2
update tsttrigger.submission set workflow_id = 1
where id = 1;

-- Id 3 not automated to next status 4
update tsttrigger.submission set workflow_id = 3
where id = 1;

-- Id 2 is automated to next status 3 THISWORK BECAUSE OF FLUSH STATEMENT
update tsttrigger.submission set workflow_id = 2
where id = 1;

-- Id 5 is automated to next status 6 THIS WILL FAILED
update tsttrigger.submission set workflow_id = 5
where id = 1;

-- Id 1 not automated to next status 2
update tsttrigger.submission set workflow_id = 1
where id = 1;

SELECT * FROM tsttrigger.submission_log;
If I'm running the flush statement before each update, the error never appear and the triggers run perfectly.

Thanks in advanced for your help.
Steve



Edited 3 time(s). Last edit at 01/24/2014 12:59PM by Steve Landry.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.