簡體   English   中英

Laravel:如何解決“SQLSTATE [HY000]:一般錯誤:1215 無法添加外鍵約束”

[英]Laravel: How to solve “SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint”

我正在進行遷移,其中存在使用外鍵將一個表與其他各種表映射的特定遷移。 並且在運行遷移時出現以下錯誤。

SQLSTATE [HY000]:一般錯誤:1215 無法添加外鍵約束(SQL:alter table bookmaps添加約束bookmaps_subject_id_foreign外鍵( subject_id )在刪除級聯時引用subjectid ))

這是創建錯誤的遷移:

    public function up()
    {
        Schema::create('bookmaps', function (Blueprint $table) {
            $table->unsignedBigInteger('book_id')->unique();
            $table->unsignedBigInteger('subject_id')->nullable();
            $table->unsignedBigInteger('grade_id')->nullable();
            $table->unsignedBigInteger('author_id')->nullable();
            $table->unsignedBigInteger('catagory_id')->nullable();
            $table->unsignedBigInteger('language_id')->nullable();
            $table->timestamps();
        });

        Schema::table('bookmaps', function($table) {
            $table->foreign('book_id')->references('id')->on('book')->onDelete('cascade');
            $table->foreign('subject_id')->references('id')->on('subject')->onDelete('cascade');
            $table->foreign('grade_id')->references('id')->on('grade')->onDelete('cascade');
            $table->foreign('author_id')->references('id')->on('author')->onDelete('cascade');
            $table->foreign('catagory_id')->references('id')->on('catagories')->onDelete('cascade');
            $table->foreign('language_id')->references('id')->on('language')->onDelete('cascade');
        });
    }

其他相關表的遷移是:

    public function up()
    {
        Schema::create('book', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('discription')->nullable();
            $table->string('book_file')->nullable();
            $table->string('card_image')->nullable();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->timestamps();
        });

        Schema::table('book', function($table) {
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
     
    }

上述遷移創建的表僅由外鍵 rest 映射,以下遷移未映射。

    public function up()
    {
        Schema::create('subject', function (Blueprint $table) {
            $table->id();
            $table->string('subject_name');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('grade', function (Blueprint $table) {
            $table->id();
            $table->string('grade_name');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('author', function (Blueprint $table) {
            $table->id();
            $table->string('author_name');
            $table->string('author_discription')->nullable();
            $table->timestamps();
        });
    }
    public function up()
    {
        Schema::create('grade', function (Blueprint $table) {
            $table->id();
            $table->string('grade_name');
            $table->timestamps();
        });
    }
    public function up()
    {
        Schema::create('author', function (Blueprint $table) {
            $table->id();
            $table->string('author_name');
            $table->string('author_discription')->nullable();
            $table->timestamps();
        });
    }
    public function up()
    {
        Schema::create('catagories', function (Blueprint $table) {
            $table->id();
            $table->string('catagories_name');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('language', function (Blueprint $table) {
            $table->id();
            $table->string('language_name');
            $table->timestamps();
        });
    }

可能是什么問題呢?

您必須在書簽之前創建主題遷移。 當 Laravel 嘗試遷移書簽表時,沒有找到鏈接到主題表的外鍵。

暫無
暫無

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

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