【问题标题】:Laravel 7 Migration - Foreign Key created but its constraint code not workingLaravel 7 迁移 - 创建了外键但其约束代码不起作用
【发布时间】:2020-06-23 16:40:25
【问题描述】:

我似乎无法弄清楚为什么我的相同类型的 PK 和 FK 没有在 Laravel 中链接。 两个表都是 InnoDB。 第二个屏幕截图中的外键显示,即使 PK 表中不存在,我也可以输入任何整数。请帮忙。我的 FK 创建迁移代码如下:

证明 PK 和 FK 类型相同(左侧为会议桌;右侧为参与者桌:

完整性约束可以被打破的证明(我可以输入 9999999 等。我应该只能输入 1、2 或 3 作为 FK ID):

迁移文件:

  <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateParticipantsTable extends Migration
    {
        public function up()
        {
            
            Schema::create('participants', function (Blueprint $table) {
    
                $table->id();
                $table->unsignedBigInteger('meeting_id')->nullable();
                $table->string('attendance');
                $table->string('invited');
    
    
                $table->foreign('meeting_id')
                ->references('id')
                ->on('meetings')
                ->onDelete('cascade');
    
                $table->timestamps();
               // $table->engine = "InnoDB";
    
            });
        }
    
     
        public function down()
        {
            Schema::dropIfExists('participants');
        }
    }

【问题讨论】:

    标签: mysql laravel-7 referential-integrity


    【解决方案1】:

    我想通了。在 /config/database.php 文件中,您必须在创建表之前 设置数据库的存储引擎。奇怪的是我在之后更改了数据库的存储引擎我之前创建了一些其他表并且没有任何问题:

    'engine' => null,
    

    'engine' => 'InnoDB',
    

    【讨论】:

      猜你喜欢
      • 2013-09-10
      • 2020-12-23
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 2016-01-20
      • 2021-04-21
      • 2021-04-06
      • 1970-01-01
      相关资源
      最近更新 更多