spatie / laravel-permission

Associate users with roles and permissions
https://spatie.be/docs/laravel-permission
MIT License
12.18k stars 1.78k forks source link

unable to use EfficientUuid with this package #1420

Closed jcherrabi closed 4 years ago

jcherrabi commented 4 years ago

hello there: I'm using EfficientUuid in my application and when I tried to do $user->assignRole($someRole) I get the following error: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'model_uuid' at row 1 (SQL: insert intomodel_has_roles(mo del_type,model_uuid,role_id) values (App\User, f86dfee1-2eaf-4656-8d8f-c696c969da26, 4))

as it is trying to insert the string representation of the UUID instead of the binary representation.. can you please suggest how to fix this?

thank you,

drbyte commented 4 years ago

Having not used that other package I can't speak to its specifics, but looking at the error message it seems you can probably fix the issue by changing the table schema so that the field is big enough to handle the length of UUID your package is generating.

In the addendum docs about using this Spatie package with general UUID use it suggests using $table->uuid() in the initial migration. But if your other package uses something longer than what the default ->uuid() call creates, you can easily add another migration to make it big enough to suit your needs.

jcherrabi commented 4 years ago

I think the problem is the sync() method used in assignRole(...) in particular this code $this->roles()->sync($roles, false); as it ignores the model's mutator casting...

since I'm casting my uuid to EfficientUuid::class, as in protected $casts = [ 'uuid' => EfficientUuid::class, ]; in my Model, which seems to be ignored.. and the assignRole tries to Sync the uuid as a string instead of a binary which is incompatible with the column type since it is set to BINARY.

any suggestion on how to fix it?

drbyte commented 4 years ago

A somewhat similar issue was posted in https://github.com/spatie/laravel-permission/issues/1434 which at first looked specific to pgsql, but turned out to be a misconfiguration, where the config/permissions.php hadn't been updated to use the extended \App models where UUID support had been implemented. This mismatch caused Laravel's sync() to behave according to defaults, and not honor the updated data types.

Perhaps yours is similar? You haven't posted enough detail to replicate the situation though.