我正在尝试迁移整个Laravel应用程序,当我在下面编写代码时,出现了一些错误。
// 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']);
});
错误:
SQLSTATE [42000]:语法错误或访问冲突:1059标识符名称'permission_user_user_id_permission_id_user_type_project_id_unique'太长(SQL:alter table
permission_user
添加唯一的permission_user_user_id_permission_id_user_type_project_id_unique
(user_id
,permission_id
,user_type
,project_id
) )`当我删除上面的代码时,一切正常。怎么了?我什么都没找到?
最佳答案
laravel生成唯一密钥的方式太长。您可以这样做
$table->unique(['user_id', 'permission_id', 'user_type', 'project_id'], 'my_unique_ref');
另外,如果外键太长,则可以覆盖默认名称,但要提供第二个参数。
$table->foreign('permission_id', 'my_new_reference')->references('id')->on('permissions')
->onUpdate('cascade')->onDelete('cascade');