MySQL错误代码:1215。无法添加外键约束。我看不出错误

时间:2016-03-02 08:15:09

标签: mysql

我有着名的1215错误,但我找不到原因......

我已经看过几个主题MySQL Error 1215: Cannot add foreign key constraint,我按照他们的所有提示,但我没有看到错误....

(Squema由MySQL Workbench自动生成......)

使用以下结构,对于表“modalidad”,我得到了:

Error code 1215: Cannot add foreign key constraint

代码:

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,ALLOW_INVALID_DATES';


DROP TABLE IF EXISTS `qvh056`.`centro` ;
CREATE TABLE IF NOT EXISTS `qvh056`.`centro` (
  `id` INT NOT NULL COMMENT 'codigo',
  `nombre` VARCHAR(45) NULL,
  `orden` INT NULL,
  `activo` TINYINT(1) NULL DEFAULT 1,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

DROP TABLE IF EXISTS `qvh056`.`compania` ;
CREATE TABLE IF NOT EXISTS `qvh056`.`compania` (
  `id` INT NOT NULL,
  `codigo` INT NOT NULL COMMENT 'Codigo contable. Se puede identificar a la compañia con empresa y codigo.',
  `nombre` VARCHAR(100) NOT NULL,
  `alias` VARCHAR(100) NULL,
  `centro_id` INT NOT NULL DEFAULT 0 COMMENT '\"empresa\"',
  PRIMARY KEY (`id`),
  INDEX `fk_compania_centro1_idx` (`centro_id` ASC),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC),
  CONSTRAINT `fk_compania_centro1`
    FOREIGN KEY (`centro_id`)
    REFERENCES `qvh056`.`centro` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


DROP TABLE IF EXISTS `qvh056`.`modalidad` ;
CREATE TABLE IF NOT EXISTS `qvh056`.`modalidad` (
  `id` CHAR(4) NOT NULL COMMENT 'codigo + centro\n',
  `codigo` CHAR(1) NOT NULL,
  `nombre` VARCHAR(45) NOT NULL,
  `centro_id` INT NOT NULL,
  `modality` CHAR(2) NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC),
  INDEX `fk_modalidad_centro1_idx` (`centro_id` ASC),
  CONSTRAINT `fk_modalidad_centro1`
    FOREIGN KEY (`centro_id`)
    REFERENCES `qvh056`.`centro` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

0 个答案:

没有答案