如何解决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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。