framework: SQLSTATE[42000] fix not working for Laravel 5.8
- Laravel Version: 5.8.3
- PHP Version: 7.3.2
- Database Driver & Version: MariaDB 10.1.38
Description:
Fix for 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))
from https://laravel.com/docs/master/migrations#creating-indexes is not working for latest Laravel
Steps To Reproduce:
- fresh Laravel 5.8.3 installation
- App\Providers\AppServiceProvider:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Fix for MySQL < 5.7.7 and MariaDB < 10.2.2
// https://laravel.com/docs/master/migrations#creating-indexes
Schema::defaultStringLength(191);
}
}
- execute
php artisan make:auth
- create new database for your connection (don’t change config files - only
.env
file) - execute
php artisan migrate
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 4
- Comments: 16 (3 by maintainers)
Commits related to this issue
- fixed string length issue https://github.com/laravel/framework/issues/27806 — committed to slefevre/patch by slefevre 4 years ago
i think the problem is that by default laravel uses the utf8mb4 charset which uses more space than the utf8 charset. You can either change the mysql settings to allow for bigger keys or just change the
charset
toutf8
and thecollation
toutf8_general_ci
inconfig/database.php
under the mysql section. Had this problem more than once and not just with laravel.also have a look at @devcircus’ comment below.
The docs should cover everything you need:
Thanks @justicenode it work for me
If you encounter this error its because your mysql(MariaDB) Version Framework has nothing to do with it
If You add
Schema::defaultStringLength(191);
toAppServiceProvider
All the fileds in the migration will have the length of191
so if you dont want ot specify the length of the colunm for each table you can add toAppserviceProvider
work for me thanks