简体   繁体   中英

Laravel Database Foreign Key Constraints not working

I'm a beginner in Laravel and I think I might have missed something really important.

I am trying to put Foreign Key Constraints in my database. I have tried to migrate my work (php artisan migrate) and it goes to the database.

However, when I want to see the "connexion" between my primary key and my foreign key on mysql, nothing shows up.

When I test it by adding an article and a tag, I can add the wrong ID on the database.

Tag Table:

    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTagTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tag', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tag');
    }
}

Article Table:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticleTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('article', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->longText('description');
            $table->longText('content');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('article');
    }
}

Article Tag Table:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticleTagTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('article_tag', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('article_id');
            $table->unsignedInteger('tag_id');
        });

        Schema::table('article_tag', function($table){
            $table->foreign('article_id')->references('id')->on('article');
            $table->foreign('tag_id')->references('id')->on('tag');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('article_tag');
    }
}

My test with the article

My test with the tag

My test with the article_tag

The table article_tag should not work with ID 1 and ID 3.

Thanks for your help!

  <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticleTagTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('article_tag', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('article_id');
        $table->unsignedInteger('tag_id');
    });


        $table->foreign('article_id')->references('id')->on('article');
        $table->foreign('tag_id')->references('id')->on('tag');



/**
 * Reverse the migrations.
 *
 * @return void
 */
 public function down()
 {
     Schema::dropIfExists('article_tag');
 }
}

Update the database configuration and set the engine value to InnoDB

config > database.php

'engine' => 'InnoDB',

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