我正在尝试在预订 table 中的受训者 table“身份”字段和 "trainee_identity" 字段之间创建外键,使用 laravel 8
am trying to create a foregin key between trainees table 'identity" field and "trainee_identity" field in bookings table , using laravel 8
学员table:
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();
});
预订table:
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();
});
迁移时出现此错误:
SQLSTATE[HY000]: 一般错误:3780 在外键约束中引用列 'trainee_identity' 和引用列 'identity' 'bookings_trainee_identity_
外国'是不相容的。 (SQL: alter table bookings
add constraint bookings_trainee_identity_foreign
foreign key (trainee_identity
) references trainees
(
identity
))
您在无符号整数中引用了有符号整数。
在trainees
table中$table->integer('identity')->unique();
是有符号的,在bookings
中table $table->integer('trainee_identity')->unsigned();
是无符号的。
两者应该是同一类型才能正常工作。
您可以将 $table->integer('identity')->unique();
更改为 $table->unsignedInteger('identity')->unique();
以使其也无符号
学员table:
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();
});
预订table:
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();
});
迁移时出现此错误:
SQLSTATE[HY000]: 一般错误:3780 在外键约束中引用列 'trainee_identity' 和引用列 'identity' 'bookings_trainee_identity_
外国'是不相容的。 (SQL: alter table bookings
add constraint bookings_trainee_identity_foreign
foreign key (trainee_identity
) references trainees
(
identity
))
您在无符号整数中引用了有符号整数。
在trainees
table中$table->integer('identity')->unique();
是有符号的,在bookings
中table $table->integer('trainee_identity')->unsigned();
是无符号的。
两者应该是同一类型才能正常工作。
您可以将 $table->integer('identity')->unique();
更改为 $table->unsignedInteger('identity')->unique();
以使其也无符号