【问题标题】:Making a column reference 2 columns from separate tables laravel从单独的表中创建列引用 2 列 laravel
【发布时间】:2018-11-19 00:26:35
【问题描述】:

我正在尝试获取 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 个外键?我只是有语法错误还是不可能这样做。

【问题讨论】:

  • 我不熟悉 laravel,但在我看来,您正试图让单个字段引用两个不同表中的相应字段?虽然这是可能的,但这绝不是一个好主意。
  • 也就是说...看起来您可能需要停止尝试使 varchar 字段“无符号”。 ;)
  • 我删除了未签名的。然后它给了我一个我能够修复的错误。我没有收到关于分配外键方式的新错误。我已经更新了问题
  • 你必须索引外键引用的字段(并且它们必须存在,所以必须先创建引用的表);但正如我在原始评论中所说,让一个字段引用多个表通常是一个非常糟糕的主意。如果您认为它的意思是“必须匹配 tableA 或 tableB 中的值”,它不会;这意味着该值必须存在于两个表中(并且取决于参考选项,可以从任何一个“级联”更改)。

标签: php mysql laravel-5 foreign-keys laravel-migrations


【解决方案1】:

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

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

【讨论】:

  • 为什么要添加index()unsigned()
  • 我试过了,没有成功。我只是想知道它背后的原因。
  • 因为一个外键整数必须是和他的表上的索引并且是无符号的
  • 所以在所有表​​上,键都必须被索引和无符号?还是只是身份验证表?
  • @Jay 通常,不会在字符串值上创建外键;它是允许的,但与整数相比相对昂贵。无符号部分不是必需的(即使是整数),但必须对外键引用的字段进行索引。
猜你喜欢
  • 2017-10-09
  • 2015-01-05
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 2016-10-11
  • 2016-02-26
相关资源
最近更新 更多