Hillcow
Hillcow

Reputation: 969

Laravel migration: Specified key was too long; max key length is 767 bytes

I have a migration like this:

    Schema::table('table', function (Blueprint $table) {
        $table->string('description', 500)->index()->change();
    });

But I get this error:

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `...` add index `tags_description_index`(`description`))  

What I have tried:

(1) change ->index() to index("t_index"). Same error. (2) In AppServiceProvider:

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

Same error.

Not sure what to do. I did not get the error on my local environment, just now that I am trying to deploy.

Upvotes: 0

Views: 741

Answers (1)

ELWAHAB
ELWAHAB

Reputation: 126

You should change type field, because type string has max length 256. Please, change type on text or longtext

For example:

 Schema::table('table', function (Blueprint $table) {
        $table->text('description', 500)->index()->change();
    });

You should read this article https://laravel.com/docs/5.8/migrations#creating-columns

Upvotes: 1

Related Questions