7

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 

her is my first tables:

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

the second table with the foreign key

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

3 Answers3

10

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

you should change:

  $table->unsignedBigInteger('userId');

to

 $table->uuid('userId')->nullable(false);
OMR
  • 11,736
  • 5
  • 20
  • 35
  • I did as you said and it accepts inserting data, but still, the two tables are not connected together I can add data to table 2 without having the user in table one! it should say no user with this Id – mdeveloper Aug 14 '20 at 12:21
  • have you removed: $table->foreign('userId') ->references('id') ->on('users') ->onDelete('cascade'); – OMR Aug 14 '20 at 12:23
  • you may try: $table->uuid('userId')->nullable(false); – OMR Aug 14 '20 at 12:25
  • no i did not remove $table->foreign('userId') ->references('id') ->on('users') ->onDelete('cascade'); why i do need to remove it ? – mdeveloper Aug 14 '20 at 12:47
  • I was just making sure of that ... the relation should be builded right.. did you remigrate your migrations? – OMR Aug 14 '20 at 14:16
  • thanks for the response, but I have a question should I add belongs to or has many statements in the model ?? – mdeveloper Aug 15 '20 at 09:40
  • for User Model: User HasMany of model TableTwo, for TableTwo it belongsTo User Model – OMR Aug 15 '20 at 09:46
  • the foreign key is not working not , i now i that has many and belong to thing but the tables are not connected at all – mdeveloper Aug 15 '20 at 13:22
3

change your code from

$table->unsignedBigInteger('userId');

to

$table->foreign('userId')

if use laravel 8 you can typing like that

$table->foreignUuid('user_id');
  • i am having the same problem but i already tried everything but still showing the same error, i already try foreignUuid(), foreign(), but still nothing works – M Andre Juliansyah Jun 29 '22 at 02:19
0

In case somebody gets to this question, now laravel allows to use the foreignUuid method:

$table->foreignUuid('userId')
    ->constrained()
    ->cascadeOnDelete();
Manuel Guzman
  • 483
  • 5
  • 15