Laravel数据库外键约束不起作用

时间:2018-03-30 13:19:16

标签: php mysql laravel

我是Laravel的初学者,我想我可能错过了一些非常重要的东西。

我正在尝试将外键约束放入我的数据库中。 我试图迁移我的工作(php artisan migrate),然后进入数据库。

然而,当我想看到" connexion"我的主键和我在mysql上的外键之间没有任何显示。

当我通过添加文章和标签对其进行测试时,我可以在数据库中添加错误的ID。

标签表:

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

文章表:

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

文章标签表:

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

表article_tag不应与ID 1和ID 3一起使用。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

if self._bbox_queue:
    bbox_queue = self._bbox_queue
else:
    painter.eraseRect(self.rect())  # this is where a Rectangle is 'called'
    bbox_queue = [
            Bbox([[0, 0], [self.renderer.width, self.renderer.height]])]