php SQLSTATE[42000]:运行laravel migration时出现错误或访问冲突标识符名称太长[重复]

cwdobuhd  于 12个月前  发布在  PHP
关注(0)|答案(2)|浏览(119)

此问题在此处已有答案

Laravel migration primary (or key) "Identifier name is too long"(2个答案)
2天前关闭。
在我的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');
    }
}

字符串
但是当我尝试运行这个的时候,我得到了下面的错误,
PDO异常::(“SQLSTATE[42000]:1059标识符名称'schedules_department_id_user_id_schedule_type_id_company_id_added_by_schedule_start_date_schedule...'太长”)

oxiaedzo

oxiaedzo1#

Laravel已经生成了索引名称。但是索引名称太长。MySQL对每个索引名称的限制是64个字符。
但是你可以设置自己的索引名,它是“foreign”方法的第二个参数。

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

字符串
范例:

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

7xllpg7q

7xllpg7q2#

请注意,你必须首先声明字段。它必须被声明为unsignedBigKey;因为laravel中的主键被声明为unsigned,所以forign key也必须是unsigned。

$table->unsignedBigInteger('date_of_handing_over_cpis_id');

字符串
然后将该字段声明为forign key。

$table->foreign('date_of_handing_over_cpis_id', 'fk_date_of_handing_over_cpis_id')
        ->references('id')->on('date_of_handing_over_cpis')
        ->cascadeOnDelete()->cascadeOnUpdate();

相关问题