我有两个表,在两个表中,我都使用 UUID 来生成一个 id。
之后,我尝试在第二个表中使用一个 ID 作为外来 ID。如图所示
迁移确实接受我正在做的事情,但是当我插入数据时,我收到此错误
Illuminate/Database/QueryException with message 'SQLSTATE[01000]: Warning: 1265 Data truncated for column 'userId' at row 1
她是我的第一张 table : 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);
关于laravel 7 使用 uuid 外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63412258/