使列引用来自单独表的2列Laravel

使列引用来自单独表的2列Laravel

我正在尝试获取2个表,它们都具有名为email passwordisBusiness的列,并在一个名为authentication的表中自动更新这些值

认证迁移

Schema::create('authentication', function (Blueprint $table) {
        $table->increments('auth_id');
        $table->string('email');
        $table->foreign('email')->references('email')->on('businesses');
        $table->foreign('email')->references('email')->on('consumers');
        $table->string('password');
        $table->foreign('password')->references('password')->on('businesses');
        $table->foreign('password')->references('password')->on('consumers');
        $table->integer('isBusiness');
        $table->foreign('isBusiness')->references('isBusiness')->on('businesses');
        $table->foreign('isBusiness')->references('isBusiness')->on('consumers');
        $table->timestamps();
    });


错误

 SQLSTATE[HY000]: General error: 1005 Can't create table `sprout_db`.`#sql-2
  7a4_30` (errno: 150 "Foreign key constraint is incorrectly formed")


业务表

Schema::create('businesses', function (Blueprint $table) {
        $table->increments('bus_id', 11);
        $table->string('bus_name', 50);
        $table->string('bus_address', 50);
        $table->string('bus_city', 50);
        $table->string('bus_prov', 50);
        $table->string('bus_postal', 50);
        $table->string('bus_phone', 50);
        $table->string('email', 50);
        $table->string('password', 100);
        $table->integer('isBusiness')->default('1');
        $table->timestamps();
        $table->rememberToken();
        $table->engine = 'InnoDB';
    });


消费者表

Schema::create('consumers', function (Blueprint $table) {
            $table->increments('con_id', 11);
            $table->string('con_fname', 50);
            $table->string('con_lname', 50);
            $table->string('email', 50);
            $table->string('password', 100);
            $table->integer('isBusiness')->default('0');
            $table->timestamps();
            $table->rememberToken();
            $table->engine = 'InnoDB';
        });


这是正确的做法吗?通过为每列分配2个外键?我是否只有语法错误,或者这样做是不可能的?

最佳答案

只需将index()方法添加到您的密钥即可。

    $table->integer('isBusiness')->index()->unsigned();

关于php - 使列引用来自单独表的2列Laravel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50769174/

10-14 13:47