1

Estoy trabajando en laravel passport con personal tokens, el id de mis usuarios es de tipo uuid para evitar que puedan ser adivinados.

Todo funciona bien en relación al usuario, el problema viene a la hora de generar un token:

$token = $user->createToken('My Token', ['read-phrase'])->accessToken;

Cuando la id de mi usuario es un int todo funciona bien, pero cuando es uuid obtengo el siguiente mensaje de error:

Illuminate\Database\QueryException: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'tokenable_id' at row 1 

He reemplazado en todas las migraciones el tipo de dato por uuid, para ser más especificos:

//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');

Pila de seguimiento del error:

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}

¿Qué mas debo cambiar para que funcione la creación de tokens con mi id de usuario como UUID?

5
  • ¿Qué tipo de dato es la columna donde almacenas el uuid?
    – user128299
    Commented el 15 oct. 2020 a las 15:17
  • @BetaM En las migraciones definí todo como uuid, en la base de datos se traduce en char(36). La generación de claves uuid la estoy realizando con el paquete de Ramsey\Uuid\Uuid. Commented el 15 oct. 2020 a las 15:21
  • 2
    La longitud del tipo de dato no es la adecuada, tu uuids ocupan un espacio mayor al que la definición da permiso por eso los trunca, deberás o ampliar la longitud del dato actual (si lo permite) o usar otro tipo de dato (prueba con esto que te digo como opción)
    – user128299
    Commented el 15 oct. 2020 a las 15:26
  • @BetaM Me he confundido, todo este tiempo he estado mirando otra tabla, al revisar la tabla, en realidad en la migración está como $table->morphs('tokenable'); lo que se traduce en la base de datos como dos columnas tokenable_type varchar(255) y tokenable_id bigint(20). Creo que llegado a este punto el error se cuenta solo. He reemplazado el tipo por uuidMorphs() y ahora todo parece funcionar bien en relación a la generación del token. Commented el 15 oct. 2020 a las 15:40
  • 1
    Deberías publicar la solución para no dejar abandonada la pregunta y que otros y otras con esa duda encuentren solución
    – user128299
    Commented el 15 oct. 2020 a las 15:42

1 respuesta 1

3

La solución fue cambiar el tipo de dato en la migración personal_access_tokens

Originalmente la columna está definida como:

$table->morphs('tokenable');

Lo que se traduce en agregar dos nuevas columnas con los siguientes tipos de datos:

  1. (_id) = UNSIGNED BIGINT
  2. (_type) = VARCHAR

La solución fue reemplazarlo por:

$table->uuidMorphs('tokenable');

Lo que genera dichas columnas como:

  1. (_id) = CHAR(36)
  2. (_type) = VARCHAR(255)

Si hay más dudas respecto al tipo de dato puede revisar Laravel Migrations

2
  • 1
    No olvides marcarla como aceptada cuando el sistema te lo permita
    – user128299
    Commented el 15 oct. 2020 a las 16:15
  • Gracias, me ayudo mucho y funciono perfecto con laravel 8
    – Ivandez
    Commented el 1 abr. 2021 a las 14:14

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.