2013-02-20 183 views
1

我研究了這個問題,它通常涉及列名不匹配。我檢查並重新檢查了我的專欄名稱,以確保它們匹配。我無法弄清楚它爲什麼在抱怨。也許我需要的只是第二雙眼睛,還是長時間的午餐休息?MySQL後插入觸發器「未知列」

表:

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' 
+0

嘗試先創建'Project'表,然後創建'User_Project'表以避免外鍵衝突。 – Slowcoder 2013-02-20 22:16:12

+0

數據庫已經啓動並運行(正確)...這是我關注的觸發器 – 2013-02-20 22:19:17

回答

3

我不是100%的這種積極的,是導致文件並沒有真正進入這個,但我認爲你只打算使用

SET NEW.lastUpdate = NOW(); 

或類似SET以設置插入行的字段值在BEFORE觸發。

嘗試將該部分拆分爲BEFORE INSERT觸發器並查看它是否有效。也許在後觸發上下文中,像這樣在記錄字段上調用SET會更新行並丟失導致錯誤的記錄指針。

我這樣說是因爲MySQL文檔只明確說明你可以在前觸發期間得到一行值SET。從文檔中查看此報價:

In a BEFORE trigger, you can also change its value with SET NEW.col_name = value if you have the UPDATE privilege for it. This means you can use a trigger to modify the values to be inserted into a new row or that are used to update a row.

+0

這是錯誤的,並且是錯誤的原因。肯定需要爲此更強大的錯誤信息。 – 2013-02-20 22:43:50

+0

是的。在MySQL中已經報告了一些錯誤,將錯誤消息更改爲更具體。 – Slowcoder 2013-02-20 22:46:00