簡體   English   中英

@Laravel遷移:無法在laravel中添加外鍵約束

[英]@Laravel Migration: Cannot add foreign key constraint in laravel

我試圖在Laravel中創建外鍵,但是當我使用artisan遷移表時,拋出以下錯誤:

λ php artisan migrate
Migration table created successfully.


[Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `fees` add constraint `fee
  s_fee_type_id_foreign` foreign key (`fee_type_id`) references `feetypes` (`fee_type_id`))


  [PDOException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

我的遷移代碼是這樣的:

費用

<?php

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

class CreateFeesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('fees', function (Blueprint $table) {
            $table->increments('fee_id');
            $table->integer('academic_id')->unsigned();
            $table->integer('level_id')->unsigned();
            $table->integer('fee_type_id');
            $table->string('fee_heading', 100)->nullable();
            $table->float('amount', 8, 2);
            $table->foreign('academic_id')->references('academic_id')->on('academics');
            $table->foreign('level_id')->references('level_id')->on('levels');
            $table->foreign('fee_type_id')->references('fee_type_id')->on('feetypes');
        });
    }

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

fesstypes

<?php

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

class CreateFeetypesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('feetypes', function (Blueprint $table) {
            $table->unsignedinteger('fee_type_id');
            $table->string('fee_type', 100);
        });
    }

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

關於我做錯了什么的任何想法,我想立即得到,因為我需要創建很多表,例如,Users,Students,Leves等。理想情況下,我想創建包含此表的表具有外鍵的數據,即費用和費用類型。

希望有人可以幫助我入門。

替換此行:

$table->integer('fee_type_id');

有了這個:

$table->unsignedInteger('fee_type_id');

CreateFeesTable遷移中。

檢查所有數據類型是否與引用的數據類型匹配。

public function up()
{
    Schema::create('fees', function (Blueprint $table) {
        $table->increments('fee_id');
        $table->unsignedInteger('academic_id');
        $table->unsignedInteger('level_id');
        $table->unsignedInteger('fee_type_id');
        $table->string('fee_heading', 100)->nullable();
        $table->float('amount', 8, 2);
        $table->foreign('academic_id')->references('academic_id')->on('academics');
        $table->foreign('level_id')->references('level_id')->on('levels');
        $table->foreign('fee_type_id')->references('fee_type_id')->on('feetypes');
    });
}

檢查級別中的level_id,學者中的Academic_id,費用類型中的fee_type_id也是unsignedIntegerautoincrement ,並將您的表創建腳本更改為上面的腳本。

CreateFeesTable.php

更改

$table->integer('fee_type_id');

$table->unsignedInteger('fee_type_id');

CreateFeetypesTable

更改

$table->unsignedinteger('fee_type_id');

$table->increments('fee_type_id'); $table->integer('fee_type_id');

首先必須創建在引用的列,即索引fee_type_idfeetypes表。

MySQL需要在外鍵和引用鍵上建立索引,以便外鍵檢查可以快速進行,而無需進行表掃描。 在引用表中,必須有一個索引,其中外鍵列以相同的順序列為第一列。 如果這樣的索引不存在,則會在引用表上自動創建。 如果您創建另一個可用於強制外鍵約束的索引,則以后可能會靜默刪除該索引。 如果給定,則使用index_name(如前所述)。

MySQL參考手冊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM