微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决我正在尝试使用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();,以使其也未签名

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。