[英]SQLSTATE[HY000]: errno: 150 "Foreign key constraint is incorrectly formed
I try to make migration and my error is "errno: 150 "Foreign key constraint is incorrectly formed" I cant explain more than this but I should write smt for stack validate length 我尝试进行迁移,我的错误是“错误:150”外键约束形成错误“我无法解释更多但我应该写smt for stack validate length
and its my code: 和我的代码:
public function up()
{
Schema::create('bus_lines', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title',20)->collation('utf8_persian_ci');
$table->unsignedInteger('code');
$table->integer('start_station');
$table->integer('end_station');
$table->smallInteger('pub');
$table->smallInteger('rmv');
$table->timestamps();
});
}
public function up()
{
Schema::create('station_buses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title',30);
$table->unsignedInteger('code');
$table->timestamps();
});
}
public function up()
{
Schema::create('busline_stationbus', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('line_code');
$table->unsignedInteger('station_code');
$table->timestamps();
$table->foreign('line_code')->references('code')->on('bus_lines')->onDelete('cascade');
$table->foreign('station_code')->references('code')->on('station_buses')->onDelete('cascade');
});
}
So if the foreign key is applied to a non primary key it has to be applied to a unique column: 因此,如果外键应用于非主键,则必须将其应用于唯一列:
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table;
FOREIGN KEY约束不必仅链接到另一个表中的PRIMARY KEY约束; it can also be defined to reference the columns of a UNIQUE constraint in another table.
它也可以定义为引用另一个表中UNIQUE约束的列。
So your code
in both tables should be this: 所以两个表中的
code
应该是这样的:
$table->unsignedInteger('code')->unique();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.