Laravel Database Migration Error Key too long
When doing a database migration on Laravel, i get following error
1 2 3 4 5 6 7 8 |
$ php artisan migrate [Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `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 $ |
This is because MariaDB use different UTF8 format.
To fix, edit file
1 |
vi ./app/Providers/AppServiceProvider.php |
Inside, find
1 2 3 4 |
public function boot() { // } |
Replace with
1 2 3 4 |
public function boot() { Schema::defaultStringLength(191); } |
You will also need to add
1 |
use Illuminate\Support\Facades\Schema; |
here is how my file look like after editing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
$ cat ./app/Providers/AppServiceProvider.php <?php namespace App\Providers; use Illuminate\Support\Facades\Schema; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); } /** * Register any application services. * * @return void */ public function register() { // } } $ |