4
votes

[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

6

6 Answers

7
votes

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);
}
2
votes
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
votes

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.

0
votes

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
votes

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.

0
votes

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);
}