【问题标题】:Laravel - Foreign key constraint is incorrectly formedLaravel - 外键约束的格式不正确
【发布时间】:2017-10-04 20:12:43
【问题描述】:

我正在尝试在 laravel 中创建表 'teams' 和 'competitions' 但是当我运行 migrate 命令时,我得到以下信息: errno: 150 "Foreign key constraint is wrongly forms"

Schema::create('competitions', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('name')->unique();
                    $table->string('team_name');
                    $table->foreign('team_name')->references('name')->on('teams')->onDelete('cascade');
                    $table->timestamps();
        });

Schema::create('teams', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('name')->unique();
                    $table->string('place');
                    $table->string('competition')->nullable();;
                    $table->foreign('competition')->references('name')->on('competitions')->onDelete('set null');
                    $table->timestamps();
});

【问题讨论】:

    标签: php html mysql laravel


    【解决方案1】:

    主键外键必须使用相同的属性

    eg. 两者都有 ->nullable() 或没有。

    另外,请确保两个表及其列的排序规则相同。

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 2019-07-25
      • 2018-02-19
      • 2021-05-26
      • 2020-09-24
      • 2019-11-02
      • 2023-03-19
      • 2020-04-15
      • 1970-01-01
      相关资源
      最近更新 更多