gpt4 book ai didi

Mysql 外键错误 150

转载 作者:行者123 更新时间:2023-11-30 00:40:33 26 4
gpt4 key购买 nike

我有这个

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

*这是我的错误。这些也是我的 user 和 msg_messages_users 表。我已经检查了所有表格,但看不到我的错误。我在 MysqlWorbench 上工作,它生成了这个错误的语法。*

Error 1005: Can't create table 'beta.msg_messages' (errno: 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;

最佳答案

我是个傻瓜...我在错误的表中声明了外键..

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;

谢谢大家

关于Mysql 外键错误 150,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21801623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com