运行 迁移时如何修复外键错误
How to fix foreign key error when running migration
我不知道为什么它仍然不起作用并显示这个:
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
);
}
第一个table
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');
}
}
第二个table
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');
}
}
您的外键需要与其引用的键具有相同的类型。因此,第二个 table 外键 doba
需要更改为此,因为您在第一个 table 主键中使用 BigIncrements()
。
$table->unsignedBigInteger('doba');
这里的问题是 druh_knihies
中的 id_druhu
被定义为 bigIncrements
而你创建 doba
为 unsignedInteger
并且在使用外键类型时应该完全一样。所以在这种情况下而不是
$table->unsignedInteger('doba');
你应该使用
$table->unsignedBigInteger('doba');
我不知道为什么它仍然不起作用并显示这个:
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 tablebooks
add constraintbooks_doba_foreign
foreign key (doba
) referencesdruh_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
);
}
第一个table
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');
}
}
第二个table
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');
}
}
您的外键需要与其引用的键具有相同的类型。因此,第二个 table 外键 doba
需要更改为此,因为您在第一个 table 主键中使用 BigIncrements()
。
$table->unsignedBigInteger('doba');
这里的问题是 druh_knihies
中的 id_druhu
被定义为 bigIncrements
而你创建 doba
为 unsignedInteger
并且在使用外键类型时应该完全一样。所以在这种情况下而不是
$table->unsignedInteger('doba');
你应该使用
$table->unsignedBigInteger('doba');