I am working in Laravel passport with personal tokens, the id of my users is of type uuid
to prevent them from being guessed.
Everything works fine in relation to the user, the problem comes when generating a token:
$token = $user->createToken('My Token', ['read-phrase'])->accessToken;
When my user id is a int
everything works fine, but when it is uuid
I get the following error message:
Illuminate\Database\QueryException: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'tokenable_id' at row 1
I have replaced in all migrations the data type by uuid
, to be more specific:
//oauth_auth_codes
$table->uuid('user_id')->index();
$table->uuid('client_id');
//oauth_access_tokens
$table->uuid('user_id')->nullable()->index();
$table->uuid('client_id');
//oauth_clients
$table->uuid('user_id')->nullable()->index();
//oauth_personal_access_clients
$table->uuid('client_id');
Error trace stack:
Illuminate\Database\QueryException: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'tokenable_id' at row 1 (SQL: insert into `personal_access_tokens` (`name`, `token`, `abilities`, `tokenable_id`, `tokenable_type`, `updated_at`, `created_at`) values (My Token, 5eaef6980a34301b04b26602d92ed224bf17867121d5f352b60b738ee5af3feb, ["read-phrase"], 00d1783b-6904-492a-a249-b4e85a3ec70c, App\Models\User, 2020-10-15 15:12:49, 2020-10-15 15:12:49)) in file C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 671
#0 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Connection.php(631): Illuminate\Database\Connection->runQueryCallback('insert into `pe...', Array, Object(Closure))
#1 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Connection.php(465): Illuminate\Database\Connection->run('insert into `pe...', Array, Object(Closure))
#2 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Connection.php(417): Illuminate\Database\Connection->statement('insert into `pe...', Array)
#3 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Query\Processors\Processor.php(32): Illuminate\Database\Connection->insert('insert into `pe...', Array)
#4 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2871): Illuminate\Database\Query\Processors\Processor->processInsertGetId(Object(Illuminate\Database\Query\Builder), 'insert into `pe...', Array, 'id')
#5 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(1496): Illuminate\Database\Query\Builder->insertGetId(Array, 'id')
#6 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(892): Illuminate\Database\Eloquent\Builder->__call('insertGetId', Array)
#7 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(857): Illuminate\Database\Eloquent\Model->insertAndSetId(Object(Illuminate\Database\Eloquent\Builder), Array)
#8 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(720): Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder))
#9 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\HasOneOrMany.php(298): Illuminate\Database\Eloquent\Model->save()
#10 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Support\helpers.php(263): Illuminate\Database\Eloquent\Relations\HasOneOrMany->Illuminate\Database\Eloquent\Relations\{closure}(Object(Laravel\Sanctum\PersonalAccessToken))
#11 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\HasOneOrMany.php(299): tap(Object(Laravel\Sanctum\PersonalAccessToken), Object(Closure))
#12 C:\xampp74\htdocs\phrases\vendor\laravel\sanctum\src\HasApiTokens.php(49): Illuminate\Database\Eloquent\Relations\HasOneOrMany->create(Array)
#13 C:\xampp74\htdocs\phrases\app\Http\Controllers\AccountsController.php(38): App\Models\User->createToken('My Token', Array)
#14 [internal function]: App\Http\Controllers\AccountsController->login(Object(Illuminate\Http\Request))
#15 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(54): call_user_func_array(Array, Array)
#16 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('login', Array)
#17 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Route.php(255): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\AccountsController), 'login')
#18 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Route.php(197): Illuminate\Routing\Route->runController()
#19 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Router.php(691): Illuminate\Routing\Route->run()
#20 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#21 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#23 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(127): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#24 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(103): Illuminate\Routing\Middleware\ThrottleRequests->handleRequest(Object(Illuminate\Http\Request), Object(Closure), Array)
#25 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(55): Illuminate\Routing\Middleware\ThrottleRequests->handleRequestUsingNamedLimiter(Object(Illuminate\Http\Request), Object(Closure), 'api', Object(Closure))
#26 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), 'api')
#27 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Router.php(693): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#29 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Router.php(668): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#30 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Router.php(634): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#31 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Routing\Router.php(623): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#32 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(166): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#33 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#34 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#35 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#36 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#37 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#39 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#40 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php(87): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#41 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure))
#42 C:\xampp74\htdocs\phrases\vendor\fruitcake\laravel-cors\src\HandleCors.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#43 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#44 C:\xampp74\htdocs\phrases\vendor\fideloper\proxy\src\TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#45 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#46 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#47 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(141): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#48 C:\xampp74\htdocs\phrases\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#49 C:\xampp74\htdocs\phrases\public\index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#50 {main}
What else do I need to change to make token creation with my userid as UUID work?
The solution was to change the data type in the migration
personal_access_tokens
Originally the column is defined as:
$table->morphs('tokenable');
Which translates to adding two new columns with the following data types:
The solution was to replace it with:
$table->uuidMorphs('tokenable');
Which outputs such columns as:
If there are more doubts regarding the type of data, you can review Laravel Migrations