-3

I am trying to migrate database in laravel, If I'm adding index() in migration/2014_10_12_100000_create_password_resets_table.php I got error like this

My-PC MINGW64 /c/xampp/htdocs/ecoprotect
$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max k
ey length is 767 bytes (SQL: alter table `password_resets` add index `password_resets_email_index`(`email`))

  at C:\xampp\htdocs\ecoprotect\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 by
tes")
      C:\xampp\htdocs\ecoprotect\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\ecoprotect\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  Please use the argument -v to see more details.

This is my code migration/2014_10_12_100000_create_password_resets_table.php:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePasswordResetsTable extends Migration
{
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }
}
rajkumar
  • 61
  • 2
  • 14

3 Answers3

7

I have found two solutions for this error.

OPTION 1:

Open your user and password_reset table in database/migrations folder

And just change the length of the email:

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

OPTION 2:

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

Open 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);
}
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
5

Add the below code in app\provider\AppServiceProvider.php method:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}
Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
  • i got this error `Illuminate\Database\QueryException : SQLSTATE[HY000] [1049] Unknown database 'example' (SQL: select * from information_schema.tables where table_schema = example and table_name = migrations)` – rajkumar Oct 17 '18 at 11:14
  • the database example doesn't exist.. update your .env file and restart the server – Salman Zafar Oct 17 '18 at 11:16
3

This issue is answered too many times in the internet, Please add the below code in appServiceProvider.php. It will solve your issue

use Illuminate\Support\Facades\Schema;

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