MySQL AFTER INSERT触发“未知列”

时间:2013-02-20 22:03:17

标签: mysql triggers

我研究了这个问题,它通常与列名不匹配有关。我检查并重新检查了我的列名,以确保它们匹配。我无法弄清楚为什么它在抱怨。也许我需要的只是第二双眼睛,或长时间的午休时间?

表格

DROP TABLE IF EXISTS `User`;
CREATE TABLE `User` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `userType` varchar(20) NOT NULL,
  `firstName` varchar(20) NOT NULL,
  `lastName` varchar(40) NOT NULL,
  `email` varchar(40) DEFAULT NULL,
  `contributorRating` int(1) DEFAULT NULL,
  `dateCreated` timestamp DEFAULT CURRENT_TIMESTAMP,
  `isFlagged` bit(1) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `userType` (`userType`),
  CONSTRAINT `User_ibfk_1` FOREIGN KEY (`userType`) REFERENCES `UserType` (`userType`)  ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `User_Project`;
CREATE TABLE `User_Project` (
  `projectID` int(11) NOT NULL,
  `userID` int(11) NOT NULL,
  `joinDate` timestamp DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`projectID`,`userID`),
  KEY `userID` (`userID`),
  CONSTRAINT `User_Project_ibfk_1` FOREIGN KEY (`projectID`) REFERENCES `Project` (`ID`) ON UPDATE CASCADE,
  CONSTRAINT `User_Project_ibfk_2` FOREIGN KEY (`userID`) REFERENCES `User` (`ID`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `Project`;
CREATE TABLE `Project` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `userID` int(11) NOT NULL,
  `projectName` varchar(40) NOT NULL,
  `frequencyType` varchar(10) DEFAULT NULL,
  `projectType` varchar(20) NOT NULL,
  `projectDescription` text NOT NULL,
  `dateCreated` timestamp DEFAULT CURRENT_TIMESTAMP,
  `startDate` timestamp DEFAULT 0,
  `endDate` timestamp DEFAULT 0,
  `isActive` tinyint(1) unsigned NOT NULL,
  `visibility` varchar(20) DEFAULT 'private',
  `region` varchar(20) NOT NULL,
  `isFlagged` bit(1) DEFAULT NULL,
  `lastUpdate` timestamp DEFAULT 0,
  PRIMARY KEY (`ID`),
  KEY `userID` (`userID`),
  KEY `frequencyType` (`frequencyType`),
  KEY `projectType` (`projectType`),
  CONSTRAINT `Project_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `User` (`ID`) ON UPDATE CASCADE,
  CONSTRAINT `Project_ibfk_2` FOREIGN KEY (`frequencyType`) REFERENCES `Frequency` (`frequency`) ON UPDATE CASCADE,
  CONSTRAINT `Project_ibfk_3` FOREIGN KEY (`projectType`) REFERENCES `ProjectType` (`projectType`) ON UPDATE CASCADE,
  CONSTRAINT `Project_ibfk_4` FOREIGN KEY (`visibility`) REFERENCES `Visibility` (`visibility`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

触发:

 DROP TRIGGER IF EXISTS ins_Project;
 CREATE TRIGGER ins_Project AFTER INSERT ON `Project`
 FOR EACH ROW 
 BEGIN
  SET NEW.lastUpdate = NOW();
  INSERT IGNORE INTO User_Project(projectID,userID,joinDate) VALUES (NEW.ID,NEW.userID,NOW());
 END;

错误:

ERROR 1054 (42S22): Unknown column 'NEW.ID' in 'field list'

1 个答案:

答案 0 :(得分:3)

我对此并非100%肯定,因为文档并没有真正涉及到这一点,但我认为你只是打算使用

SET NEW.lastUpdate = NOW();

或类似SETBEFORE触发器中的插入行设置字段值。

尝试将该部分拆分为BEFORE INSERT触发器,看看它是否有效。也许在后触发器上下文中,在记录字段上调用SET会更新行并丢失导致错误的记录指针。

我之所以这样说是因为MySQL文档只明确指出在前触发期间你可以SET一个行值。请查看文档中的引用:

  

在BEFORE触发器中,如果您具有UPDATE权限,还可以使用SET NEW.col_name = value更改其值。这意味着您可以使用触发器来修改要插入新行或用于更新行的值。