davy_yg's avatar
Level 27

table migration

Hello,

upon migration I am getting this error message:

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 password_resets add index password_resets_email_index(email))

What do I need to change?

migrations/create_password_resets_table.php

class CreatePasswordResetsTable extends Migration
{
    /**
    * Run the migrations.
    *
    * @return void
    */

public function up() { Schema::create('password_resets', function (Blueprint $table) { $table->string('email')->index(); $table->string('token'); $table->timestamp('created_at')->nullable(); }); }

0 likes
8 replies
Snapey's avatar

or just change the length of the email address in your migration file.

in create_users migration

            $table->string('email',191)->unique();
1 like
UdhavSarvaiya's avatar
Level 2

Open your user and password_reset table in database/migrations folder

And just change the length of the email:

$table->string('email',191)->index();

And if you want to limit the length of every string in your project then follow this

Edit your app/Providers/AppServiceProvider.php file and inside the boot() method set a default string length:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}
Snapey's avatar

@udhavsarvaiya and @szach why would you want to limit the length of every string when only those with unique rule have issues?

1 like
Szach's avatar

@snapey If I recall correctly, It happens on the first time I run php artisan migrate on a new project or try to post something on to the database.

Snapey's avatar

I know exactly what the problem is. It is because the unique attribute on the email column creates an index that is too long. Your solution is to set the default string length for EVERY string in every migration to a shorter default when the only issue is with the email address. Who needs an email address field more than about 50 characters, never mind more than 191

Through the issue on the framework, I lobbied for the migration to be changed with the simple addition of a more realistic string length but this was knocked back by those that just said upgrade your database.

1 like
Szach's avatar

@snapey Interesting to know about it being related to unique attribute on email column. Since it was the recommended way, I suggested it to help the OP, since I was stuck at the same place. I think information like you have mentioned about it being related to email could be posted to guides under this error since many who will encounter this or have encountered this would not know it was related to unique in email like you do.

Please or to participate in this conversation.