问题描述
我有两个表,两者都在使用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);