简体   繁体   中英

Laravel Migration MySQL 1215: Cannot add foreign key constraint

I have issue with a foreign key that I can't add.

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');
});

The two commented lines work.

But the wishpseudo return a 1215 Mysql error.

Thanks

You can find the SQL script here

--
-- 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;

I have the error on http://sqlfiddle.com/ but I don't find the error, I need help...

I don't understand what you are trying to achieve. But try calling

$table->foreign('wishpseudo')->references('wishpseudo')->on('relation'); this in new Schema::create() so something like this:

 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');
    });

You can try below code:

$table->integer('wishpseudo')->unsigned();
$table->foreign('wishpseudo')->references('id')->on('relation');

I hope this will work for you

Schema::create('match_applicant', function (Blueprint $table) {
    $table->string('wishpseudo')->references('wishpseudo')->on('relation');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM