3 つのテーブル間に外部キーを設定しようとしていますが、タイトルにエラーが表示されます。_id フィールドはすべて署名されておらず、親と同じ形式です。
問題のテーブル:
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 Workbench によって実行されるクエリ
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@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 SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_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