我遇到了无法添加的外键问题。
Schema::create('relation', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->string('applicantpseudo');
$table->string('wishpseudo');
$table->timestamps();
$table->boolean('incall')->default('0');
$table->primary(['applicantpseudo', 'wishpseudo']);
});
Schema::create('match_applicant', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->string('pseudo');
$table->string('applicantpseudo');
$table->string('wishpseudo');
$table->boolean('match')->default('0');
$table->primary(['pseudo', 'applicantpseudo', 'wishpseudo']);
// $table->foreign('pseudo')->references('pseudo')->on('users');
// $table->foreign('applicantpseudo')->references('applicantpseudo')->on('relation');
$table->foreign('wishpseudo')->references('wishpseudo')->on('relation');
});
两条注释行都有效。
但是wishpseudo返回一个1215 Mysql错误。
谢谢
您可以在此处找到SQL脚本
--
-- Structure de la table `relation`
--
DROP TABLE IF EXISTS `relation`;
CREATE TABLE IF NOT EXISTS `relation` (
`applicantpseudo` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`wishpseudo` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`incall` tinyint(1) NOT NULL DEFAULT '0',
CONSTRAINT pk_relation PRIMARY KEY (`applicantpseudo`,`wishpseudo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Structure de la table `match_applicant`
--
DROP TABLE IF EXISTS `match_applicant`;
CREATE TABLE IF NOT EXISTS `match_applicant` (
`pseudo` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`applicantpseudo` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`wishpseudo` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`match` tinyint(1) NOT NULL DEFAULT '0',
CONSTRAINT pk_relation PRIMARY KEY (`pseudo`,`applicantpseudo`,`wishpseudo`),
CONSTRAINT test FOREIGN KEY (wishpseudo) REFERENCES relation (wishpseudo)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
我在http://sqlfiddle.com/上遇到了错误,但没有找到错误,我需要帮助...
答案 0 :(得分:0)
我不明白您要达到的目标。但是尝试致电
$table->foreign('wishpseudo')->references('wishpseudo')->on('relation');
在新的Schema::create()
中是这样的,
Schema::create('relation', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->string('applicantpseudo');
$table->unsignedInteger('wishpseudo');
$table->timestamps();
$table->boolean('incall')->default('0');
$table->primary(['applicantpseudo', 'wishpseudo']);
});
Schema::create('match_applicant', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->string('pseudo');
$table->string('applicantpseudo');
$table->string('wishpseudo');
$table->boolean('match')->default('0');
$table->primary(['pseudo', 'applicantpseudo', 'wishpseudo']);
// $table->foreign('pseudo')->references('pseudo')->on('users');
// $table->foreign('applicantpseudo')->references('applicantpseudo')->on('relation');
});
Schema::create('match_applicant', function (Blueprint $table) {
$table->foreign('wishpseudo')->references('wishpseudo')->on('relation');
});
答案 1 :(得分:0)
您可以尝试以下代码:
$table->integer('wishpseudo')->unsigned();
$table->foreign('wishpseudo')->references('id')->on('relation');
答案 2 :(得分:0)
我希望这对您有用
Schema::create('match_applicant', function (Blueprint $table) {
$table->string('wishpseudo')->references('wishpseudo')->on('relation');
});