laravel 7用作uuid外键

问题描述

我有两个表,两者都在使用UUID生成ID。 之后,我试图在第二个表中使用一个id作为外来对象。如图所示 迁移确实接受我在做什么,但是当我插入数据时出现此错误

Illuminate/Database/QueryException with message 'SQLSTATE[01000]: Warning: 1265 Data truncated for column 'userId' at row 1 

她是我的第一张桌子:

       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();
        });

第二个带有外键的表

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

解决方法

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

您应该更改:

  $table->unsignedBigInteger('userId');

 $table->uuid('userId')->nullable(false);

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...