小编Cha*_*eel的帖子

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

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

Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 3780 Referencing column 'room_id' and referenced column 'id' in foreign key constraint 'contacts_room_id_foreign' are incompatible.")
      /Users/shaquilenoor/Desktop/chatapi/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

  2   PDOStatement::execute()
      /Users/shaquilenoor/Desktop/chatapi/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
Run Code Online (Sandbox Code Playgroud)

根据异常跟踪中包含的代码,问题似乎在以下代码中:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateContactsTable extends Migration
{
    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();
            $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');
        });
    }

    public function down()
    {
        Schema::dropIfExists('contacts');
    }
}
Run Code Online (Sandbox Code Playgroud)

关于如何解决的任何想法?

php terminal laravel composer-php

3
推荐指数
2
解决办法
2914
查看次数

标签 统计

composer-php ×1

laravel ×1

php ×1

terminal ×1