gpt4 book ai didi

mysql - 如何修复运行迁移时外键错误

转载 作者:行者123 更新时间:2023-11-29 09:26:15 24 4
gpt4 key购买 nike

我不知道为什么它仍然不起作用并显示:

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table db_rocnikovka.books (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table books add constraint books_doba_foreign foreign key (doba) references druh_knihies (id_druhu))

at C:\xampp\htdocs\Rocnikovka\vendor\laravel\framework\src\Illuminate\Database\Connection.php:665

Exception trace: 1 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create > table db_rocnikovka.books (errno: 150 "Foreign key constraint is incorrectly formed")") C:\xampp\htdocs\Rocnikovka\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459

2 PDOStatement::execute()

  C:\xampp\htdocs\Rocnikovka\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459
 catch (Exception $e) {
throw new QueryException(
$query, $this->prepareBindings($bindings), $e
);
}

第一个表

class CreateDruhKnihiesTable extends Migration
{
public function up()
{
Schema::create('druh_knihies', function (Blueprint $table) {
$table->bigIncrements('id_druhu');
$table->string('nazev');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('druh_knihies');
}
}

第二个表

class CreateBooksTable extends Migration
{
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->bigIncrements('id_book');
$table->string('nazev');
$table->string('autor');
$table->string('druh');
$table->unsignedInteger('doba');
$table->foreign('doba')->references('id_druhu')->on('druh_knihies');
$table->integer('pocet_stranek');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('books');
}
}

最佳答案

您的外键需要与其引用的键具有相同的类型。因此,第二个表外键 doba 需要更改为此,因为您在第一个表主键中使用了 BigIncrements()

$table->unsignedBigInteger('doba');

关于mysql - 如何修复运行迁移时外键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59555437/

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