我有错误#150。
我在不同的论坛上寻找,但他们只是说外键必须有相同的类型。
这是我的代码:
USE `dbask`;
set names utf8;
/*users*/
CREATE TABLE IF NOT EXISTS `users` (
`id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(30) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`permission` VARCHAR(10) NULL,
`firstname` VARCHAR(40) NULL,
`lastname` VARCHAR(40) NULL,
`birthdate` VARCHAR(255) NULL,
`picture_link` VARCHAR(255) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
/*questions*/
CREATE TABLE IF NOT EXISTS `questions` (
`id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(30) NOT NULL,
`asked_username` VARCHAR(30) NULL,
`question` VARCHAR(255) NOT NULL,
`answer` VARCHAR(255) NULL,
`time_asked` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_questions_to_users`
FOREIGN KEY (`username`)
REFERENCES `users`(`username`)
ON DELETE CASCADE
ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
两种类型都是VARCHAR(30)
,所以我不知道是什么问题。