How to fix foreign key error when running migration(运行迁移时如何修复外键错误)
问题描述
我不知道为什么它仍然不起作用并显示这个:
I don't know why it's still don't work and show this:
IlluminateDatabaseQueryException : SQLSTATE[HY000]: 一般错误:1005 无法创建表 db_rocnikovka.books(错误号:150外键约束的格式不正确")(SQL:alter tablebooks 添加约束 books_doba_foreign 外键(doba)参考 druh_knihies (id_druhu))
IlluminateDatabaseQueryException : SQLSTATE[HY000]: General error: 1005 Can't create table
db_rocnikovka.books(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tablebooksadd constraintbooks_doba_foreignforeign key (doba) referencesdruh_knihies(id_druhu))
在 C:xampphtdocsRocnikovkavendorlaravelframeworksrcIlluminateDatabaseConnection.php:665
at C:xampphtdocsRocnikovkavendorlaravelframeworksrcIlluminateDatabaseConnection.php:665
异常跟踪:1 PDOException::("SQLSTATE[HY000]: 一般错误: 1005 无法创建 > 表 db_rocnikovka.books (errno: 150 "外键约束格式不正确")")C:xampphtdocsRocnikovkavendorlaravelframeworksrcIlluminateDatabaseConnection.php:459
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:xampphtdocsRocnikovkavendorlaravelframeworksrcIlluminateDatabaseConnection.php:459
2 PDOStatement::execute()
2 PDOStatement::execute()
C:xampphtdocsRocnikovkavendorlaravelframeworksrcIlluminateDatabaseConnection.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().
You foreign key needs to have the same type as the key it references. Therefor the second table foreign key doba needs to be change to this, becuase you use BigIncrements() in the first table primary key.
$table->unsignedBigInteger('doba');
这篇关于运行迁移时如何修复外键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:运行迁移时如何修复外键错误
基础教程推荐
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 无法解决整理冲突 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
