Laravel-permission: 1071 Specified key was too long

Created on 19 Oct 2018  路  2Comments  路  Source: spatie/laravel-permission

Hi I am using Laravel 5.6 and I am getting the following error;

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table model_has_permissions add index model_has_permissions_model_type_model_id_index(model_type, model_id))

I have added the following to my AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

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

However I still get the error.

Here is the code used to create the table;

Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use 
  ($tableNames) {
            $table->integer('permission_id')->unsigned();
            $table->morphs('model');

            $table->foreign('permission_id')
                ->references('id')
                ->on($tableNames['permissions'])
                ->onDelete('cascade');

            $table->primary(['permission_id', 'model_id', 'model_type']);
        });

I'm assuming the problem is in regards to the model_type column being created with the primary key, however I have no idea how to fix it?

support

Most helpful comment

Why are you using 200 instead of 191?

https://laravel-news.com/laravel-5-4-key-too-long-error

All 2 comments

Why are you using 200 instead of 191?

https://laravel-news.com/laravel-5-4-key-too-long-error

Thank you, I hadn't realised that I had set it to 200. That seems to be the problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ionesculiviucristian picture ionesculiviucristian  路  4Comments

younus93 picture younus93  路  4Comments

notflip picture notflip  路  3Comments

MichalKrakow picture MichalKrakow  路  4Comments

holymp2006 picture holymp2006  路  4Comments