user

Jo Micheal

20 Jan 2022

[solved] SQLSTATE[42000]: Syntax error or access violation

Laravel

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') )

I get this error when i try to run the migration using 

php artisan migrate

what should I do to figure out this Error?

Comments

Joseph Morgan

20 Jan 2022

Best Answer

best answer

that's because the defaultStringLength is not setup, you need to limit it from /app/Providers/AppServiceProvider.php
using the Schema 

so the file should looks like 

<?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(){
 //
 Schema::defaultStringLength(191);
 }
}

I imported the schema using use Illuminate\Support\Facades\Schema;
then added Schema::defaultStringLength(191); into the boot function
Let me know if this worked with you

Replies

Jo Micheal

20 Jan 2022

Thanks, this was the issue, when i updated the file like this it worked fine 

© 2024 Copyrights reserved for web-brackets.com