我正在尝试迁移整个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_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
,{{1 }},permission_id
,user_type
))`
删除上面的代码后,一切正常。怎么了?我什么都没找到?!
答案 0 :(得分:1)
laravel生成唯一密钥的方式太长。您可以通过
覆盖它RegionBase
此外,如果外键太长,您可以覆盖默认名称,但要提供第二个参数。
$table->unique(['user_id', 'permission_id', 'user_type', 'project_id'], 'my_unique_ref');