php - Lumen 5.6 Migrate Error Specified key was too long max key length is 767 bytes

870

I use Lumen 5.6 and mysql. when i type "php artisan migrate" following error occur:

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t  
oo long; max key length is 767 bytes (SQL: alter table `users` add unique `  
users_email_unique`(`email`))  

I put following code into "boot" method in the AppServiceProvider

Schema::defaultStringLength(191);

but I didn't achieve to any success.

509

Answer

Solution:

you just need one more step

go to app.php on bootstrap folder and uncomomment or modif this line

// $app->register(App\Providers\AppServiceProvider::class);

to this code

$app->register(App\Providers\AppServiceProvider::class);

have a good day

232

Answer

Solution:

You need couple of things to do. I also faced this issue and fixed it by following these two steps

  1. Go to app.php in bootstrap directory and uncomment or modify this line.

    // $app->register(App\Providers\AppServiceProvider::class);
    
  2. Now you need to defineboot()function inAppServiceProviderfile

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

Then you are good to go!

959

Answer

Solution:

Go to config in filedatabase.php then edit

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

to

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

Answer

Solution:

use Illuminate\Support\Facades\Schema; //AppServiceProvider.php

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

//rollback your migration or delete all table from database then migrate again.
575

Answer

Solution:

  1. in bootstrap/app.php un-comment this line:
$app->register(App\Providers\AppServiceProvider::class);
  1. in app/AppServiceProvider.php add below public function toAppServiceProvider class:
public function boot()
  {
    Schema::defaultStringLength(191);
  }
566

Answer

Solution:

Known to work in Laravel/Lumen 7.x:

I've tried the un-commenting ofAppServiceProvider::class and other solutions mentioned above, but the following worked for me.

If you look in/vendor/laravel/lumen-framework/config/database.php forcharset andcollation, the code checks your.env file and resorts toutf8mb4 andutf8mb4_unicode_ci, respectively.

If your database' charset is set toutf8 and collation toutf8_unicode_ci, simply add the following to your.env file:

# .env
...
DB_CHARSET=utf8
DB_COLLATION=utf8_unicode_ci
...

People are also looking for solutions to the problem: php - How to pass an array with key into view using Template Parser in CodeIgniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.