![](/img/trans.png)
[英]Laravel 8 Migration “General error: 1215 Cannot add foreign key constraint”
[英]Laravel Migration MySQL 1215: Cannot add foreign key constraint
我有一個我無法添加的外鍵問題。
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/上有錯誤,但我沒有找到錯誤,我需要幫助......
我不明白你想要實現的目標。 但試着打電話
$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');
});
你可以嘗試下面的代碼:
$table->integer('wishpseudo')->unsigned();
$table->foreign('wishpseudo')->references('id')->on('relation');
我希望這對你有用
Schema::create('match_applicant', function (Blueprint $table) {
$table->string('wishpseudo')->references('wishpseudo')->on('relation');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.