X
    Categories: Laravel

Specified key was too long – Laravel migration

Specified key was too long – Laravel migration

Specified key was too long Laravel migration, I have installed laravel 5.4 and run php artisan migrate in terminal, but I have faced with the error in terminal. I dint face with these error in my previous laravel basic installation. After a long research have found the solution.

Steps I did :

Step 1 : create-project –prefer-dist laravel/laravel laravel5-4
Step 2 : Changed database details in database.php
Step 3 : php artisan migrate

 Error I faced :

[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 `users` add unique `users_ema  
  il_unique`(`email`)) 

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

 Have found the 2 kind of solution for this :


 Solution 1 :

Add below 2 lines in database.php

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

My file :

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'mysql'),

    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',

Solution 2 :

Add charset and collation for your table in your migration file

$table->charset = 'utf8'; 
$table->collation = 'utf8_unicode_ci';

For eg :

 <?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->charset = 'utf8'; 
            $table->collation = 'utf8_unicode_ci';
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone_number');
            $table->text('address');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Hope it will help someone.

Marimuthu: