php - Laravel 迁移名称太长

标签 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]: 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 unique permission_user_user_id_permission_id_user_type_project_id_unique(user_id, permission_id, user_type, project_id))`

当我删除上面的代码时,一切正常。怎么了?我没有找到任何东西?!

最佳答案

laravel 生成唯一 key 的方式太长。您可以通过执行

来覆盖它
$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');

关于php - Laravel 迁移名称太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52246703/

相关文章:

php - 如何管理mysql中每天5000条记录的插入更新?

php - PayPal Rest-api-sdk-php 与 Laravel 4

php - MySQL 行数据到 PHP 数组键/值

php - 如何通过 PHP 运行 SQL 更新查询?

mysql - MySQL 中两种模式匹配形式的区别

Laravel 编码实践/最优化的存储方法

jquery - Laravel 5.8 require 函数在 app.js 中找不到

php - 如何检查用户是否有权限查看文档

php mysql 结果每次都从 php 返回错误,但在 sql pro 和 phpmyadmin 中没问题

php - 优惠券代码系统架构设计建议