4

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter tabl e users add unique users_email_unique(email))

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

whats wrong? im using laravel 5.3

muzamil indra
  • 137
  • 3
  • 12
  • 1
    Possible duplicate of [Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes](https://stackoverflow.com/questions/42244541/laravel-migration-error-syntax-error-or-access-violation-1071-specified-key-wa) – Ashutosh Sharma May 21 '18 at 07:28

6 Answers6

7

Refering to Laravel News and Laravel's migration guide:

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Support\Facades\Schema;

function boot()
{
    Schema::defaultStringLength(191);
}
SteD
  • 13,331
  • 12
  • 60
  • 74
2
open create_user_tabel and then write one line inside up function.
  Schema::defaultStringLength(191);
e.g

    public function up()
    {      
      Schema::defaultStringLength(191);

    Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
    }

It will work 100%.

0

That error comes from the database basically..If you try to increase the length of that column by phpmyadmin, you will get the same error.

Muhammad Rizwan
  • 438
  • 7
  • 20
0

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Support\Facades\Schema;

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

After that everything should work as normal.

0

In case, the above solutions or the official solution which is adding

Schema::defaultStringLength(191);

doesn't work. Try to edit the database.php file in config folder. Just edit

'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',

to

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

and it should work. Hope it helps.

Koushik Das
  • 5,412
  • 3
  • 29
  • 29
0

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

use Illuminate\Support\Facades\Schema;

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