创建具有外键约束的表的Mysql错误150

时间:2014-06-09 18:45:58

标签: mysql

我在以下create语句中收到错误150:

CREATE TABLE `lazarus`.`warehouses_devices` (
  `parent_unit_id` INT UNSIGNED NULL DEFAULT NULL,
  `child_unit_id` INT UNSIGNED NULL DEFAULT NULL,
  `message_type` VARCHAR(255) NULL,
  `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `value` LONGTEXT NOT NULL,
  INDEX `parent_unit_id` (`parent_unit_id` ASC),
  INDEX `child_unit_id` (`child_unit_id` ASC),
  INDEX `message_type` (`message_type` ASC),
  INDEX `date` (`date` ASC),
  CONSTRAINT `fk_parent_unit_id_unit_id`
    FOREIGN KEY (`parent_unit_id`)
    REFERENCES `lazarus`.`logs` (`unit_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci

而且我不确定为什么,我查找错误150并且它说明它的外键问题而且我真的很困惑,因为原因不是很清楚。

确切错误为ERROR 1005: Can't create table 'lazarus.warehouses_devices' (errno: 150)

2 个答案:

答案 0 :(得分:1)

考虑到您已经创建了一个名为logs的表 其中unit_id设置为主键。很可能由于引用列和引用列之间的数据类型不匹配而导致错误。

然后,您可能需要检查parent_unit_id中的warehouses_devices列与logsunit_id)中的数据类型是否相同。

确保lazaruswarehouses_devices parent_unit_id INT UNSIGNED

logs表中,unit_id必须为INT UNSIGNED

此外,在您的CREATE TABLE lazarus.warehouses_devices (中,您正在创建一个名为的约束 fk_parent_unit_id_unit_id。确保你没有在另一个表中创建了同名的另一个约束。

答案 1 :(得分:0)

根据documentation

  

InnoDB目前不支持表的外键   用户定义的分区。这意味着没有用户分区的InnoDB   table可能包含引用的外键引用或列   外键。

logs已分区,而warehouses_devices则未分区。

相关问题