带外键的Mysql错误150

时间:2014-02-15 18:14:48

标签: mysql foreign-keys foreign-key-relationship mysql-error-1005

我有这个

CREATE  TABLE IF NOT EXISTS `beta`.`msg_messages` (
  `id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
  `id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
  `subject` VARCHAR(200) NOT NULL ,
  `body` TEXT NOT NULL ,
  `date` DATETIME NOT NULL ,
  PRIMARY KEY (`id_msg`) ,
  INDEX fk_msg_messages_user (`id_user_from` ASC) ,
  INDEX fk_msg_messages_msg_messages_users (`id_msg` ASC) ,
  CONSTRAINT `fk_msg_messages_user`
    FOREIGN KEY (`id_user_from` )
    REFERENCES `beta`.`user` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_msg_messages_msg_messages_users`
    FOREIGN KEY (`id_msg` )
    REFERENCES `beta`.`msg_messages_users` (`id_msg` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB

*这是我的错误。这些也是我的用户和msg_messages_users表。我已经查看了所有表格,但我无法看到我的错误。我正在使用MysqlWorbench,它会产生这个错误的sintax。*

  

错误1005:无法创建表'beta.msg_messages'(错误号:150)。

CREATE  TABLE IF NOT EXISTS `beta`.`user` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
  `username` VARCHAR(100) NOT NULL ,
   PRIMARY KEY (`id`) ,
ENGINE = InnoDB
AUTO_INCREMENT = 87
DEFAULT CHARACTER SET = utf8;

 CREATE  TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
      `id` BIGINT UNSIGNED NOT NULL ,
      `id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
      `id_msg` BIGINT(20) UNSIGNED NOT NULL ,
      `status` TINYINT NOT NULL DEFAULT 0 ,
      `date` DATETIME NOT NULL ,
      PRIMARY KEY (`id`) ,
      INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
      CONSTRAINT `fk_msg_messages_users_user`
        FOREIGN KEY (`id_usr_to` )
        REFERENCES `beta`.`user` (`id` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB;

1 个答案:

答案 0 :(得分:0)

我是个笨蛋......我在错误的桌子上宣布一把外键......

CREATE  TABLE IF NOT EXISTS `beta`.`msg_messages` (
  `id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
  `id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
  `subject` VARCHAR(200) NOT NULL ,
  `body` TEXT NOT NULL ,
  `date` DATETIME NOT NULL ,
  PRIMARY KEY (`id_msg`) ,
  INDEX fk_msg_messages_user (`id_user_from` ASC) ,
  CONSTRAINT `fk_msg_messages_user`
    FOREIGN KEY (`id_user_from` )
    REFERENCES `beta`.`user` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE  TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
  `id_msg` BIGINT(20) UNSIGNED NOT NULL ,
  `id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
  `status` TINYINT NOT NULL DEFAULT 0 ,
  `date` DATETIME NOT NULL ,
  INDEX id_msg (`id_msg` ASC) ,
  INDEX fk_msg_messages_users_msg_messages (`id_msg` ASC) ,
  INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
  CONSTRAINT `fk_msg_messages_users_msg_messages`
    FOREIGN KEY (`id_msg` )
    REFERENCES `beta`.`msg_messages` (`id_msg` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_msg_messages_users_user`
    FOREIGN KEY (`id_usr_to` )
    REFERENCES `beta`.`user` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

感谢所有