PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes") code example

Example 1: laravel migation error

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

Example 2: PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes") laravel 8

// Update your /app/Providers/AppServiceProvider.php to contain:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

//ON this error 
//   PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists")
// After run ->  php artisan migrate:fresh  <- ! Note this will reset all tables in db

Example 3: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

Example 4: ERROR 1071 (42000) at line 76: Specified key was too long; max key length is 767 bytes laravel

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

Example 5: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes laravel 8

Some Solution that worked for me: (Try running command after config:clear and
                                   try migrate:fresh)
1. added default string length:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

2. changing engine to 'InnoDB' inside /config/database.php
  
'mysql' => [
    ...,
    ...,
    'engine' => 'InnoDB',
 ]
  
3. If still not solved try updating 'charset' and 'collation' inside 
  /config/database.php
  
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',

to

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

Tags:

Misc Example