laravel迁移名称太长

u91tlkcl  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(322)

我正在尝试迁移整个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\u user\u user id\u permission\u id\u user\u type\u project\u id\u 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 ))`
当我删除上面的代码时,一切正常。怎么了?我什么都没找到?!

isr3a4wc

isr3a4wc1#

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');

相关问题