1

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!

5
  • 1
    I believe the default table engine on your MySQL is MyISAM.. use query SHOW ENGINES to check that. MyISAM doesn't support Foreign Key Constraints InnoDB does support Foreign Key Constraints Commented Mar 30, 2018 at 13:25
  • 1
    Confirm that the MySQL tables are using ENGINE=InnoDB, and that FOREIGN_KEY_CHECKS is enabled. (Foreign key constraints are not enforced for ENGINE=MyISAM tables.) Commented Mar 30, 2018 at 13:28
  • 1
    Besides it's better is to add a $table->engine = 'InnoDB' to every Schema::create this way you always enforce the MySQL server to create InnoDB engine tables. Commented Mar 30, 2018 at 13:32
  • Do i have to add it to all the tables? Commented Mar 30, 2018 at 13:44
  • Thanks guys I have now another problem, but I have already find a stack overflow solved issue (SQLSTATE[HY000]: General error: 1215) Commented Mar 30, 2018 at 13:59

2 Answers 2

3

Update the database configuration and set the engine value to InnoDB

config > database.php

'engine' => 'InnoDB',
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.