繁体   English   中英

SQLSTATE[42000]:运行 laravel 迁移时语法错误或访问冲突标识符名称太长

[英]SQLSTATE[42000]: Syntax error or access violation Identifier name too long when running laravel migration

在我的 laravel 应用程序中,我有以下迁移文件

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSchedulesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('schedules', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('department_id');
            $table->foreign('department_id')->references('id')->on('departments');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');
            $table->unsignedBigInteger('company_id');
            $table->foreign('company_id')->references('id')->on('companies');
            $table->unsignedBigInteger('added_by');
            $table->foreign('added_by')->references('id')->on('users');
            $table->string('schedule_name');
            $table->integer('schedule_type_id')->unsigned();
            $table->foreign('schedule_type_id')->references('id')->on('schedule_types');
            $table->date('schedule_start_date')->nullable();
            $table->date('schedule_end_date')->nullable();
            $table->date('schedule_actual_end_date')->nullable();
            $table->time('schedule_travel_time')->nullable();
            $table->unsignedBigInteger('rotation_scheme_id');
            $table->foreign('rotation_scheme_id')->references('id')->on('schedule_rotational');
            $table->date('rotational_schedule_period_from')->nullable();
            $table->date('rotational_schedule_period_to')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index([
                'department_id', 'user_id', 'schedule_type_id', 'company_id', 'added_by','schedule_start_date',
                'schedule_end_date', 'rotation_scheme_id', 'rotational_schedule_period_from', 'rotational_schedule_period_to',
                'rotation_shift_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('schedules');
    }
}

但是当我尝试运行它时,我收到以下错误,

PDOException::("SQLSTATE[42000]: 语法错误或访问冲突:1059 标识符名称 'schedules_department_id_user_id_schedule_type_id_company_id_add_by_schedule_start_date_schedule...' 太长")

Laravel 已生成索引名称。 但索引名称太长。 MySQL 每个索引名称限制为 64 个字符。

但是您可以设置自己的索引名称。 它是“外来”方法的第二个参数。

 $table->foreign($fieldName, $indexName);

例子:

$table->foreign('user_id', 'fk_user_id')->references('id')->on('users');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM