2010-08-25 103 views
0

我在做敏捷Yii書。嘗試INSERT時MySQL和外鍵衝突

無論如何,我想執行這個命令:

INSERT INTO tbl_project_user_assignment (project_id, user_id) values ('1','1'), ('1','2'); 

而且我得到這個錯誤:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`trackstar_dev`.`tbl_project_user_assignment`, CONSTRAINT `FK_project_user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE) 

所以..我想,讓我們看看如果tbl_project表已經PROJECT_ID = 1。做了一個快速的SELECT * FROM tbl_project;並且項目存在。好吧,我們來檢查用戶,SELECT * FROM tbl_user; Yup 2用戶,ID爲1和2.

我在做什麼錯?有錯字嗎?敏捷yii書有幾個拼寫錯誤,但它們並不嚴重,而且太新,所以沒有報告勘誤(已經檢查過)。

這裏的數據庫模式從源代碼:

-- Disable foreign keys 
SET FOREIGN_KEY_CHECKS = 0 ; 

-- Create tables section ------------------------------------------------- 

-- Table tbl_project 

CREATE TABLE IF NOT EXISTS `tbl_project` (
    `id` INTEGER NOT NULL auto_increment, 
    `name` varchar(128) NOT NULL, 
    `description` text NOT NULL, 
    `create_time` DATETIME default NULL, 
    `create_user_id` INTEGER default NULL, 
    `update_time` DATETIME default NULL, 
    `update_user_id` INTEGER default NULL, 
    PRIMARY KEY (`id`) 
) ENGINE = InnoDB 
; 

-- DROP TABLE IF EXISTS `tbl_issue` ; 

CREATE TABLE IF NOT EXISTS `tbl_issue` 
( 
    `id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    `name` varchar(256) NOT NULL, 
    `description` varchar(2000), 
    `project_id` INTEGER, 
    `type_id` INTEGER, 
    `status_id` INTEGER, 
    `owner_id` INTEGER, 
    `requester_id` INTEGER, 
    `create_time` DATETIME, 
    `create_user_id` INTEGER, 
    `update_time` DATETIME, 
    `update_user_id` INTEGER 
) ENGINE = InnoDB 
; 

-- DROP TABLE IF EXISTS `tbl_user` ; 

-- Table User 

CREATE TABLE IF NOT EXISTS `tbl_user` 
(
    `id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    `email` Varchar(256) NOT NULL, 
    `username` Varchar(256), 
    `password` Varchar(256), 
    `last_login_time` Datetime, 
    `create_time` DATETIME, 
    `create_user_id` INTEGER, 
    `update_time` DATETIME, 
    `update_user_id` INTEGER 
) ENGINE = InnoDB 
; 

-- DROP TABLE IF EXISTS `tbl_project_user_assignment` ; 

-- Table User 

CREATE TABLE IF NOT EXISTS `tbl_project_user_assignment` 
(
    `project_id` Int(11) NOT NULL, 
    `user_id` Int(11) NOT NULL, 
    `create_time` DATETIME, 
    `create_user_id` INTEGER, 
    `update_time` DATETIME, 
    `update_user_id` INTEGER, 
PRIMARY KEY (`project_id`,`user_id`) 
) ENGINE = InnoDB 
; 


-- The Relationships 
ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_project` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT; 

ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_owner` FOREIGN KEY (`owner_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT; 

ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_requester` FOREIGN KEY (`requester_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT; 

ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_project_user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT; 

ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_user_project` FOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT; 

-- Insert some seed data so we can just begin using the database 
INSERT INTO `tbl_user` 
    (`email`, `username`, `password`) 
VALUES 
    ('[email protected]','Test_User_One', MD5('test1')), 
    ('[email protected]','Test_User_Two', MD5('test2'))  
; 

-- Enable foreign keys 
SET FOREIGN_KEY_CHECKS = 1 ; 

無論如何,在此先感謝!

編輯: 說明該項目確實存在^^。

mysql> select id,name from tbl_project;

+ ---- + ------------------- +

| id |名稱|

+ ---- + ------------------- +

| 6 |項目1 |

| 1 |項目殭屍1 |

+ ---- + ------------------- +

2組行(0.00秒)

+0

爲什麼PROJECT_ID和USER_ID在tbl_project_user_assignment int類型(11 )而不是INTEGER?我傾向於認爲INTEGER是4 BYTES,INT(11)會去8個BYTES。這可能有一些影響,可能不會。 – MikeAinOz 2010-08-25 23:22:50

+0

@MikeAinOz 謝謝!你修好了。這是問題所在。呃,我猜我會提交勘誤表。 – mythicalprogrammer 2010-08-25 23:30:08

回答

2

的PROJECT_ID和USER_ID在tbl_project_user_assignment中鍵入爲INT(11)而不是INTEGER。我傾向於認爲INTEGER是4 BYTES,INT(11)會去8個BYTES。

正如上面評論INTEGER修復了這個問題。

0

這是你遇到的一個奇怪的問題,也是一個奇怪的問題。據我所知,INTEGER,INT,或INT(XX)(其中XX是某個數字)之間沒有內部差異。它們都具有相同的字節存儲分配和最小/最大範圍的相同數據類型。這不應該在MySQL評估某些fk關係的類型不匹配時發揮作用。當我在一個表中使用INT(11)並且在另一個表中使用INTEGER時,MySQL(5.1.49)的版本/配置不會引發您遇到的相同約束違規。我想知道這是否與您的配置有關,或者您是否使用其他外部數據庫工具。

人們可以瞭解更多關於MySQL的數據類型這裏的內部:

http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

此頁面上特別感興趣的

Another extension is supported by MySQL for optionally specifying the display width of integer data types in parentheses following the base keyword for the type (for example, INT(4)). This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.) The display width does not constrain the range of values that can be stored in the column, nor the number of digits that are displayed for values having a width exceeding that specified for the column. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three characters are displayed using more than three characters.

+0

只是一個普通的Ubuntu盒子,服務器版本:5.1.41-3ubuntu12.6(Ubuntu)。我猜他們將它固定在5.1.49? – mythicalprogrammer 2010-09-29 00:39:54