First; according to this

I edited: app\Providers\AppServiceProvider.php

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

Second; I issued php artisan make:migration create_users

Third; I implemented the generated class:

<?php

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

class CreateUsers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
           Schema::create('users2',function($users){
           $users->increments('id');
           $users->string('email',320)->unique(); // this causes errors
           $users->string('username',100)->unique();
           $users->string('password',50);
           $users->rememberToken();
           $users->timestamps();
        });
    }

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

Fourth; after issuing php artisan migrate

I get this result:

[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 users2 add unique users2_email_unique(email))

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

The problem is in this line: $users->string('email',320)->unique();

When I checked the database; I found the email column created with the specified length 320 but NOT unique UNI

If length is set to 191 or less, then the email column is created properly with the specified length and uniqueness.

So how to allow the creation of columns which are both unique and not limited to the 191 ceiling?!

0

There are 0 answers