问题描述
学员表:
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();
});
迁移时出现此错误:
sqlSTATE [HY000]:常规错误:3780在外键约束'bookings_trainee_identity_中引用列'trainee_identity'和引用列'identity'
外国”不兼容。 (sql:alter table bookings
添加约束bookings_trainee_identity_foreign
外键(trainee_identity
)引用trainees
(
identity
)
解决方法
您用无符号整数引用有符号整数。
在trainees
表中,$table->integer('identity')->unique();
被签名,在bookings
表中, $table->integer('trainee_identity')->unsigned();
是未签名。
两者应该使用相同的类型才能起作用。
您可以将$table->integer('identity')->unique();
更改为$table->unsignedInteger('identity')->unique();
,以使其也未签名