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_permissionsadd indexmodel_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?
Why are you using 200 instead of 191?
Thank you, I hadn't realised that I had set it to 200. That seems to be the problem.
Most helpful comment
Why are you using
200instead of191?https://laravel-news.com/laravel-5-4-key-too-long-error