Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1071 Specified key was too long #909

Closed
RocoClothing opened this issue Oct 19, 2018 · 5 comments
Closed

1071 Specified key was too long #909

RocoClothing opened this issue Oct 19, 2018 · 5 comments
Labels

Comments

@RocoClothing
Copy link

RocoClothing commented Oct 19, 2018

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?

@drbyte drbyte added the support label Oct 19, 2018
@drbyte
Copy link
Collaborator

drbyte commented Oct 19, 2018

Why are you using 200 instead of 191?

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

EDIT: FYI ... v4 of this package, which adds some unique indexes in the migration, requires 125 instead of 191 if you're using MySQL 8.0. (MariaDB doesn't require any of this).

@RocoClothing
Copy link
Author

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

@msyadav88
Copy link

running "php artisan migrate" , which try to add unique to permissions table gives error of #1071 - Specified key was too long; max key length is 1000 bytes , even if I have set Schema::defaultStringLength(191); in appserviceprovider

@msyadav88
Copy link

Make sure that your storage engine is INNODB.
If yes, then you don't need to set Schema::defaultStringLength(200);

@SteveOwago
Copy link

200
Set the value to 125. This worked for me.
Schema::defaultStringLength(125);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants