3

As indicated by this person, this person and this person, in order to avoid a

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

you have to modify the AppServiceProvider.php of your Laravel project like that:

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;              //add this

class AppServiceProvider extends ServiceProvider {
    public function boot() {
        Schema::defaultStringLength(191);           //and this
    }

    public function register() { }
}

But the drawback of this method is that the VARCHARs I use are limited to 191 characters, and I want to have a field longer than that. How should I do?

Thank you for your help.

JacopoStanchi
  • 1,962
  • 5
  • 33
  • 61
  • The questions you link to has several answers about how you can configure your mysql server to support this thing (indexes longer than 191 characters). What's wrong with those? – sisve May 04 '18 at 10:57
  • 1
    Because I will deploy my project onto my client's machine and I don't know which MySQL version will be supported. Besides, I didn't know that setting the VARCHAR's length in the migration allowed to bypass the 255 characters limitation. – JacopoStanchi May 04 '18 at 12:07
  • 1
    Setting the varchar's length in the migration _does not_ get rid of the limitation. The limitation comes from trying to create an index on a table and the index becomes longer than supported. There will be no limitation if you never create an index on the varchar field. However, Laravel creates an index on the users.email field in the default migration, thus people see this limit in their new project. I think you should just document the mysql configuration as a application requirement, and let their technical team solve it for you. – sisve May 07 '18 at 08:13

2 Answers2

4

Actually on your migration file you can define the size. Example

$table->string('field_name', 200);
Dharma Saputra
  • 1,524
  • 12
  • 17