Laravel-7迁移中外键约束的格式不正确

Laravel-7迁移中外键约束的格式不正确,laravel,foreign-keys,database-migration,laravel-relations,Laravel,Foreign Keys,Database Migration,Laravel Relations,当您使用laravel迁移应用外键时,它会通过这种类型的错误进行迁移 “外键约束格式不正确” 迁移的默认结构 User Table --------- Schema::create('users', function (Blueprint $table) { $table->id(); $table->timestamps(); }); Chat Table --------- Schema::create('c

当您使用laravel迁移应用外键时,它会通过这种类型的错误进行迁移

“外键约束格式不正确”

迁移的默认结构

User Table
---------

Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });

Chat Table
---------

 Schema::create('chats', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');


        });


这是因为我们的列大小不应该完全相同,请看下面

$table->id();
This will create a big integer

如何解决此问题

$table->unsignedBigInteger('user_id');


添加unsignedBigInteger,您的问题就会得到解决。

之所以会出现这种情况,是因为我们的列大小不应该完全相同,请查看下面的内容

$table->id();
This will create a big integer

如何解决此问题

$table->unsignedBigInteger('user_id');


添加unsignedbiginger,您的问题就会得到解决。

您也可以使用
$table->foreignd('user\u id')
@apokryfos您是对的,这是来自laravel-7的。“$table->foreigned('user_id')->constrated();”您也可以使用
$table->foreigned('user_id')
@apokryfos您是对的,这是来自laravel-7的。“$table->foreigned('user_id')->constrated();”