General error: 1364 Field 'guard_name' doesn't have a default value
FrancisMawn opened this issue · 8 comments
Hi,
We've just updated from 5.5.5 to 5.5.12 and now most of our tests are failing. As I understand, the guard property is no longer set by default when creating a Role. Any idea what could have caused this change? Any pointers would be appreciated.
Versions
- spatie/laravel-permission package version: 5.5.12
- illuminate/framework package: 9.36.1
PHP version: 8.1.11
Database version: MySQL 8
To Reproduce
Steps to reproduce the behavior:
- App\Models\Role::factory()->create();
- Getting exception: SQLSTATE[HY000]: General error: 1364 Field 'guard_name' doesn't have a default value
Expected behavior
This used to work fine with versions 5.5.5 and below.
Additional context
I'm using a custom Role model class which extends the base Role model class from the package. Not sure if that would have anything to do with the issue.
You must specify the guard_name
,
if you gonna use the default one: ['guard_name' => config('auth.defaults.guard')]
@parallels999 Of course, I can manually set the guard name... But this wasn't required before. To me it seems like a breaking change was introduced between patch versions.
Maybe breaking for tests, but that change greatly improves performance at runtime,
Feel free to make a PR if you think it's necessary
Also you can set default on db field
Although this isn't a massive issue for us; having a breaking change in a patch version is still a bit annoying.
Although this isn't a massive issue for us; having a breaking change in a patch version is still a bit annoying.
I'm with you on this. Took me a while to figure out that this was coming from a bugfix version.
Do a PR reverting the change and explain the problem
Does it not make sense to simply use the creating
observer to set the value before it's persisted? That will remove the need to overload the methods while letting users use w/e creation method they choose.