【问题标题】:Laravel: How to solve "SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint"Laravel:如何解决“SQLSTATE[HY000]:一般错误:1215 无法添加外键约束”
【发布时间】:2020-10-25 02:23:20
【问题描述】:

我正在进行迁移,其中存在使用外键将一个表与其他各种表映射的特定迁移。并且在运行迁移时出现以下错误。

SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:更改表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');
        });
     
    }

上述迁移创建的表仅通过外键映射,其余迁移未映射。

    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();
        });
    }

可能是什么问题?

【问题讨论】:

  • 添加主键
  • 有没有办法让 book_id 既作为外键又作为主键?因为它是一个用于映射的表,并且像一对一的关系一样使用。
  • 添加自定义主键时,您需要在您的模型上定义它protected $primaryKey = "book_id";
  • @STA 我没有立即创建模型文件,正在考虑稍后创建它。我认为在执行迁移时它应该不会影响。
  • 是的,但是您的迁移,您需要在迁移中使用主键来建立关系

标签: php mysql laravel foreign-keys laravel-migrations


【解决方案1】:

您必须在书签之前创建主题迁移。当 Laravel 尝试迁移书签表时,它没有找到链接到主题表的外键。

【讨论】:

  • 这不是答案,请在评论中询问您的问题。不是答案
  • 这种类型的响应更适合 cmets。是的,所有其他迁移都是在 bookmaps 迁移之前创建的。
猜你喜欢
  • 2018-07-29
  • 1970-01-01
  • 2019-08-02
  • 2021-07-03
  • 2020-01-14
  • 2022-11-18
  • 2016-12-20
  • 2019-11-11
  • 2019-07-27
相关资源
最近更新 更多