我正在尝试使用laravel 8在预订表的受训者表'identity'字段和“ trainee_identity”字段之间创建一个前向键

问题描述

学员表:

    Schema::create('trainees',function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email');
        $table->string('password');
        $table->string('telephone');
        $table->string('address');
        $table->integer('identity')->unique();
        $table->timestamps();
    });

预订表:

    Schema::create('bookings',function (Blueprint $table) {
        $table->increments('id');
        $table->integer('course_id')->unsigned();
        $table->foreign('course_id')->references('id')->on('courses');
        $table->integer('trainee_identity')->unsigned();
        $table->foreign('trainee_identity')->references('identity')->on('trainees');
        $table->string('payed')->default('0');
        $table->timestamps();
    });

迁移时出现此错误

enter image description here

sqlSTATE [HY000]:常规错误:3780在外键约束'bookings_trainee_identity_中引用列'trainee_identity'和引用列'identity' 外国”不兼容。 (sql:alter table bookings添加约束bookings_trainee_identity_foreign外键(trainee_identity)引用traineesidentity

解决方法

您用无符号整数引用有符号整数。
trainees表中,$table->integer('identity')->unique();被签名,在bookings表中, $table->integer('trainee_identity')->unsigned();是未签名。
两者应该使用相同的类型才能起作用。
您可以将$table->integer('identity')->unique();更改为$table->unsignedInteger('identity')->unique();,以使其也未签名