laravel 7 使用 uuid 外键

mde*_*per 7 uuid laravel

我有两个表,在两个表中,我都使用 UUID 来生成 id。之后,我尝试在第二个表中使用一个 id 作为外来 ID。如图所示,迁移确实接受我正在做的事情,但是当我插入数据时,我收到此错误

Illuminate/Database/QueryException with message 'SQLSTATE[01000]: Warning: 1265 Data truncated for column 'userId' at row 1 
Run Code Online (Sandbox Code Playgroud)

她是我的第一张桌子:

       Schema::create('users', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('userName')->unique();
            $table->string('email')->unique();
            $table->boolean('isVerified')->default(false);
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
Run Code Online (Sandbox Code Playgroud)

第二个带有外键的表

Schema::create('tableTwo', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->unsignedBigInteger('userId');
            $table->foreign('userId')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
            $table->timestamps();
        });
Run Code Online (Sandbox Code Playgroud)

OMR*_*OMR 10

您正在将整数列映射到 uuid 列,不同类型的 sql 约束无法完成...

你应该改变:

  $table->unsignedBigInteger('userId');
Run Code Online (Sandbox Code Playgroud)

 $table->uuid('userId')->nullable(false);
Run Code Online (Sandbox Code Playgroud)