laravel-permission icon indicating copy to clipboard operation
laravel-permission copied to clipboard

Timestamps in migration for sql server

Open sabas opened this issue 1 year ago • 3 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)

sabas avatar Feb 19 '24 15:02 sabas

use flase and reader error if there is no error displayed, then try to upgrade/update your db version sql table->timestamps(false);

KNMoise avatar Feb 20 '24 09:02 KNMoise

@KNMoise care to clarify? I reported a solution to the default set up of the package :)

sabas avatar Feb 20 '24 09:02 sabas

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' => '[email protected]', //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.

KNMoise avatar Feb 20 '24 10:02 KNMoise

There are no plans to change the migration to accommodate this, as it is very driver-specific. Anyone needing the timestamps(2) format can easily update their migration accordingly.

drbyte avatar Apr 19 '24 03:04 drbyte