【发布时间】:2018-11-19 00:26:35
【问题描述】:
我正在尝试获取 2 个表,它们都有名为 email password 和 isBusiness 的列,并在一个名为 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