gpt4 book ai didi

mysql - SQLSTATE[HY000] : errno: 150 "Foreign key constraint is incorrectly formed

转载 作者:行者123 更新时间:2023-11-29 05:01:41 25 4
gpt4 key购买 nike

我尝试进行迁移我的错误是“错误号:150”外键约束不正确形成“我无法解释更多,但我应该为堆栈验证长度编写 smt

及其我的代码:

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');
});
}

最佳答案

因此,如果外键应用于非主键,则它必须应用于唯一列:

A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.

所以你在两个表中的代码应该是这样的:

$table->unsignedInteger('code')->unique();

关于mysql - SQLSTATE[HY000] : errno: 150 "Foreign key constraint is incorrectly formed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57770228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com