Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Laravel PDOException常规错误引用列和外键约束中的引用列不兼容_Php_Laravel_Terminal - Fatal编程技术网

PHP Laravel PDOException常规错误引用列和外键约束中的引用列不兼容

PHP Laravel PDOException常规错误引用列和外键约束中的引用列不兼容,php,laravel,terminal,Php,Laravel,Terminal,我目前正在通过终端在Laravel中进行迁移,在尝试使用php artisan migrate时出现以下两个错误:;我将在下面打印错误: 异常跟踪: 1 PDOException::(“SQLSTATE[HY000]:一般错误:3780引用列'room_id'和外键约束'contacts_room_id_foreign'中引用列'id'不兼容。”) /Users/shaquilenoor/Desktop/chatapi/vendor/laravel/framework/src/illusted

我目前正在通过终端在Laravel中进行迁移,在尝试使用php artisan migrate时出现以下两个错误:;我将在下面打印错误:

异常跟踪:
1 PDOException::(“SQLSTATE[HY000]:一般错误:3780引用列'room_id'和外键约束'contacts_room_id_foreign'中引用列'id'不兼容。”)
/Users/shaquilenoor/Desktop/chatapi/vendor/laravel/framework/src/illusted/Database/Connection.php:458
2语句::execute()

/Users/shaquilenoor/Desktop/chatapi/vendor/laravel/framework/src/illusted/Database/Connection.php:458在这种情况下,
room\u id
应该是未签名的。试试这个:

 public function up()
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user1_id');
            $table->unsignedInteger('user2_id');
            $table->integer('room_id')->unique()->unsigned();
            $table->timestamps();
            $table->foreign('room_id')->references('id')->on('rooms');
            $table->foreign('user1_id')->references('id')->on('users');
            $table->foreign('user2_id')->references('id')->on('users');
        });
    }

这是因为在创建房间表之前设置了外键。您可以通过执行以下操作来修复此问题


使用Illumb\Support\Facades\Schema;
使用Illumb\Database\Schema\Blueprint;
使用\Database\Migrations\Migration;
类CreateContactsTable扩展了迁移
{
公共职能
{
Schema::disableForeignKeyConstraints();
架构::创建('contacts',函数(Blueprint$表){
$table->increments('id');
$table->unsignedInteger('user1_id');
$table->unsignedInteger('user2_id');
$table->integer('room_id')->unique();
$table->timestamps();
$table->integer('room_id')->unsigned();
$table->integer('user1_id')->unsigned();
$table->integer('user2_id')->unsigned();
$table->foreign('room_id')->references('id')->on('rooms');
$table->foreign('user1_id')->references('id')->on('users');
$table->foreign('user2_id')->references('id')->on('users');
});
Schema::enableForeignKeyConstraints();
}
公共职能部门
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('contacts');
Schema::enableForeignKeyConstraints();
}
}

如果您使用的是Laravel 5.8,则新迁移将更改为大增量,因此要修复引用错误,只需将integer更改为bigInteger,例如:

$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
将更改为:

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

或者更改原始迁移 从

大增量()

公正

增量()


在外键列中执行

bigInteger()

而不是

整数()

Laravel 6.0中,迁移与提到的@Payam Khaninejad相同。i、 e

$table->bigInteger('user_id')->unsigned()->index();

我正在更新Laravel 6.0的版本,因为我遵循的旧教程显示了相同的错误。

同样在Laravel 7.x中,您可能需要更改:

$table->increments('id');//无符号整数
//到
$table->id();//无符号BIGINT

在旧版本中迁移的表似乎存在问题。我遇到了同样的问题,在我的例子中,我对每个外键使用了两种不同类型的声明。“整数”和“无符号整数”

用户表 联系人表
外键必须与引用id相同, 因此发生了变化

$table->integer('room_id')->unique();

这就成功了

PS:你会得到另一个错误

SQLSTATE[42S01]:基表或视图 已存在:1050表


只需删除表并运行
php artisan migrate

我最近在创建带有外键约束的表的迁移过程中遇到了这个问题。我发现这是由于表中使用的排序规则造成的。我们的DB模式加载了一个模式文件,该文件的表排序规则设置为
utf8mb4\u 0900\u ai\u ci
。看起来Laravel默认使用
utf8mb4\u unicode\u ci


我通过在创建过程中使用
$table->collation='YOUR\u collation\u VALUE'
设置表的排序规则解决了这个问题。对于Laravel 8.x,更改
$table->integer('user\u id')->unsigned()->index()至:

$table->unsignedbiginger('user_id')
因为它与
id()
数据类型兼容。

在您的房间迁移中,更改id本身

$table->id();
它将自动创建名为“id”的bigInteger

然后在联系人迁移中,使用

$table->bigInteger('room_id')->references('id')->on('rooms');
其他外键也一样,只需遵循上面的代码即可


至少这种语法在laravel 8上有效。请对你的答案添加一些解释,以便其他人可以从中学习-此更改与给定的错误消息有何关系?请对你的答案添加一些解释,以便其他人可以从中学习-为什么需要这样做?
$table->id()//无符号大整数
so
$table->unsignedbiginger('room\u id')
$table->integer('room_id')->unique();
$table->unsignedBigInteger('room_id')->unique();
$table->id();
$table->bigInteger('room_id')->references('id')->on('rooms');