Laravel迁移名称太长

时间:2018-09-09 16:53:56

标签: php mysql laravel migration

我正在尝试迁移整个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_uniqueuser_id,{{1 }},permission_iduser_type))`

删除上面的代码后,一切正常。怎么了?我什么都没找到?!

1 个答案:

答案 0 :(得分:1)

laravel生成唯一密钥的方式太长。您可以通过

覆盖它
RegionBase

此外,如果外键太长,您可以覆盖默认名称,但要提供第二个参数。

$table->unique(['user_id', 'permission_id', 'user_type', 'project_id'], 'my_unique_ref');