0
我試圖設置3個表之間的外鍵,並在標題中收到錯誤,所有_id字段是未簽名的,並且與其父文件的格式相同。錯誤:錯誤1005:無法創建表'網站。#sql-3e5_102'(錯誤:150)
表中的問題:
CREATE TABLE `Group` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`name_url` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`enabled` tinyint(1) unsigned NOT NULL DEFAULT '1',
`order` int(11) NOT NULL DEFAULT '0',
`internal` tinyint(1) NOT NULL,
`updated` int(11) DEFAULT NULL,
`created` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `Permission` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`description` tinytext COLLATE utf8_unicode_ci,
`controller` varchar(250) CHARACTER SET utf8 DEFAULT NULL,
`action` varchar(250) CHARACTER SET utf8 DEFAULT NULL,
`enabled` tinyint(1) unsigned NOT NULL DEFAULT '0',
`updated` int(11) DEFAULT NULL,
`created` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `Group_Permission` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`group_id` int(11) unsigned DEFAULT NULL,
`permission_id` int(11) unsigned DEFAULT NULL,
`enabled` tinyint(1) unsigned NOT NULL DEFAULT '1',
`use_client_id` tinyint(1) DEFAULT '0',
`updated` int(11) DEFAULT NULL,
`created` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `perm, group_id` (`permission_id`,`group_id`)
) ENGINE=InnoDB AUTO_INCREMENT=58 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
查詢被MySQL工作臺運行
SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @[email protected]@SQL_MODE, SQL_MODE='TRADITIONAL';
ALTER TABLE `website`.`Group`
ADD CONSTRAINT `fk_Group_Group_Permission1`
FOREIGN KEY (`id`)
REFERENCES `website`.`Group_Permission` (`group_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
ALTER TABLE `website`.`Group_Permission`
DROP INDEX `perm, group_id`
, ADD INDEX `permission_id` (`group_id` ASC)
, ADD INDEX `group_id` (`group_id` ASC) ;
ALTER TABLE `website`.`Permission`
ADD CONSTRAINT `fk_Permission_Group_Permission1`
FOREIGN KEY (`id`)
REFERENCES `website`.`Group_Permission` (`permission_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
SET [email protected]_SQL_MODE;
SET [email protected]_FOREIGN_KEY_CHECKS;
SET [email protected]_UNIQUE_CHECKS;
查詢創建外鍵:
New client connection
client connection established
Executing SQL script in server
> ERROR: Error 1005: Can't create table 'website.#sql-3e5_102' (errno:
> 150)
ALTER TABLE `website`.`Group`
ADD CONSTRAINT `fk_Group_Group_Permission1`
FOREIGN KEY (`id`)
REFERENCES `website`.`Group_Permission` (`group_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
SQL script execution finished: statements: 3 succeeded, 1 failed
https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash3/s720x720/598890_10150863098731536_1561097773_n.jpg 所以我的關係是走錯了路? – azz0r
@ azz0r是的,我會這樣說。引用的表應始終是保存主記錄的表。 –
林不知道我在工作臺出錯的地方,然後 – azz0r