我使用MySQL Workbench制作了一个ERD,现在我正在尝试导入它,
它尝试创建错误的第一个表。
sql:
CREATE TABLE IF NOT EXISTS `db`.`catagories` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`catagory` INT UNSIGNED NULL,
`order` INT NULL,
PRIMARY KEY (`id`),
INDEX `fk_catagory_idx` (`catagory` ASC),
CONSTRAINT `fk_catagory`
FOREIGN KEY (`catagory`)
REFERENCES `db`.`catagories` (`catagory`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
catagory和ID都有一个索引,都是一样的,但是仍然会抛出这个错误,有什么想法吗?
答案 0 :(得分:1)
您的外键约束无效, 列catagory引用自身。
编辑:回答以下评论。
如果要引用父类别,请参考id字段。
CREATE TABLE IF NOT EXISTS `db`.`catagories` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`parent_catagory` INT UNSIGNED NULL,
`order` INT NULL,
PRIMARY KEY (`id`),
INDEX `fk_catagory_idx` (`parent_catagory` ASC),
CONSTRAINT `fk_catagory`
FOREIGN KEY (`parent_catagory`)
REFERENCES `db`.`catagories` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;