如何在Laravel迁移中修复``双重外键约束''

时间:2019-07-04 09:49:32

标签: laravel-5.8 laravel-migrations

我想在一个表中引用两个不同的表时创建两个外键

我遇到以下错误:

        General error: 1005 Can't create table `abumela_main`.`#sql- 
        780_38` (errno: 150 "Foreign key constraint is incorrectly 
        formed") (SQL:  alter table `mails_details` add constraint 
        `mails_details_sender_id_foreign` foreign key (`sender_id`) 
         references `senders` (`sender_id`) on delete cascade on update 
         cascade)

这是我尝试过的方法,但是第一个有效,而另一个无效。

        public function up()
        {
          Schema::create('mails_details', function (Blueprint $table) {
            $table->unsignedInteger('details_id')
                ->autoIncrement();
            $table->unsignedInteger('mail_id');
            $table->foreign('mail_id')
                ->references('mail_id')
                ->on('mails') //mails tables
                ->onUpdate('cascade') //cascade on update
                ->onDelete('cascade'); //cascade on delete
            $table->string('comments');
            $table->dateTime('date_received');
            $table->unsignedInteger('sender_id');
            $table->foreign('sender_id')
                ->references('sender_id')
                ->on('senders') //mails tables
                ->onUpdate('cascade') //cascade on update
                ->onDelete('cascade'); //cascade on delete
            $table->timestamps();
          });
        }

0 个答案:

没有答案