laravel 7用作uuid外键 [英] laravel 7 using as uuid foreign key

查看:79
本文介绍了laravel 7用作uuid外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two tables, in both, I am using UUID to generate an id. after that, I am trying to use one id as a foreign in the second table. as shown the migration does accept what I am doing but when I insert data I get this error

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约束无法完成...

your are mapping an integer column to uuid column, different types do the sql constraint can't be done ...

您应该更改:

  $table->unsignedBigInteger('userId');

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

这篇关于laravel 7用作uuid外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆