Reputation: 771
I am trying to migrate the whole Laravel application, When I write the code below I have some error.
// Create table for associating permissions to users (Many To Many Polymorphic)
Schema::create('permission_user', function (Blueprint $table) {
$table->unsignedInteger('permission_id');
$table->unsignedInteger('user_id');
$table->string('user_type');
$table->unsignedInteger('project_id')->nullable();.
$table->foreign('permission_id')->references('id')->on('permissions')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('project_id')->references('id')->on('projects')
->onUpdate('cascade')->onDelete('cascade');
$table->unique(['user_id', 'permission_id', 'user_type', 'project_id']);
});
The error:
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'permission_user_user_id_permission_id_user_type_project_id_unique' is too long (SQL: alter table
permission_user
add uniquepermission_user_user_id_permission_id_user_type_project_id_unique
(user_id
,permission_id
,user_type
,project_id
))`
When I remove the code above, everything works well. What is wrong? I do not find anything?!
Upvotes: 1
Views: 4234
Reputation: 3772
The laravel's way of generating a unique key is too long. You can override this by doing
$table->unique(['user_id', 'permission_id', 'user_type', 'project_id'], 'my_unique_ref');
Also, if the foreign keys are too long you can override the default name but providing a second parameter.
$table->foreign('permission_id', 'my_new_reference')->references('id')->on('permissions')
->onUpdate('cascade')->onDelete('cascade');
Upvotes: 3