Laravel-7迁移中外键约束的形成不正确

问题描述

使用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

AND

 $table->integer('user_id');
This will create a small integer that's why Our foreign key relations fails

如何解决此问题

$table->unsignedBigInteger('user_id');

OR

$table->foreignId('user_id')->constrained();

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...