Timestamps in migration for sql server
sabas opened this issue · 4 comments
Hi,
generating the migration and running in sql server leads to an error when trying to register a permission, because Laravel timestamps are needed to be registered with timestamps2
datatype.
When detecting sql server as the database,
the fields in the migration should be written as $table->timestamps(2);
My commands
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan optimize:clear
php artisan permission:create-permission "admin"
My version
"laravel/framework": "^10.10",
"spatie/laravel-permission": "^6.3"
Error
# php artisan permission:create-permission "admin"
Illuminate\Database\QueryException
SQLSTATE[22007]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]La conversione di un tipo di dati nvarchar in datetime ha generato un valore non compreso nell'intervallo dei valori consentiti. (Connection: sqlsrv, SQL: insert into [permissions] ([guard_name], [name], [updated_at], [created_at]) values (web, admin, 2024-02-19 15:04:35.936, 2024-02-19 15:04:35.936))
(The conversion of a varchar data type to a datetime data type resulted in an out-of-range value)
use flase and reader error if there is no error displayed, then try to upgrade/update your db version sql
table->timestamps(false);
@KNMoise care to clarify? I reported a solution to the default set up of the package :)
Sorry i mistaken read the error incorrectly.
1st option
run php artisan make:seeder AdminUserSeeder
then in db - seeder
public function run()
{
$this->call(AdminUserSeeder::class);
User::create([
'name' => 'Admin',
'email' => 'admin@gmail.com', //user any email
'password' => Hash::make('password'),
])->assignRole('admin');
}
2nd Option
if doesn't work check for the name column is of type int(11). It expects an integer, not a string (VARCHAR). Change your column type (preferably with a migration) from int(11) to a VARCHAR.